How to import an xml file into an Android App?

My Android App generates a small database in XML format, and I have implemented the function to “export” this file using the ContentSharer class, but how do I import a file? What if I want to load an XML file (or any other kind of file) say from the external storage or from another location of the Android device storage? Apparently the FileChooser doesn’t work under Android.

Any advice is greatly appreciated. Thanks.

1 Like

Ok, nevermind… I was wrong, the FileChooser does work under Android but the showDialog method isn’t supported, you have to use the launchAsync method instead. I wonder why the documentation is so weak sometimes!

Here’s my code… for the sake of generations to come:

    FileChooser fc(String("Import XML file"), File(), String("*.xml"), true, false, this);
    fc.launchAsync(FileBrowserComponent::openMode | FileBrowserComponent::canSelectFiles,
    [this](const FileChooser &fc)
    {
        // Get the result
        auto result = fc.getURLResult();
        DBG("File: " << result.toString(true));
        String fileName = result.isEmpty() ? String()
            : (result.isLocalFile() ? result.getLocalFile().getFullPathName()
                : result.toString(true));

        // If we have a file...
        if (!fileName.isEmpty())
            DoImportFile(File(fileName.toRawUTF8()));
    });

And another chapter of this tale is…

…after a file is selected in the native Android File Chooser, the callback function is totally ignored. So, while it’s true that the FileChooser is invoked, it turns out to be useless. What am I doing wrong?

1 Like