Android: can't write/read files in shared folders

Juce 6.0.8. Assuming I have configured the Juce project with Read from External Storage = Enabled and Write to External Storage = Enabled, and I have implemented the runtime Permission request that of course is granted, I can now launch the file chooser and select a file to write, the URI I receive from the file chooser is: content:// that translates into the absolute path:

auto u = fc.getURLResult();
auto f = u.getLocalFile();

if (f.create().ok())
	if (f.replaceWithText("Test OK!"))
		AlertWindow::showMessageBoxAsync(AlertWindow::InfoIcon, "Info", "File saved.");
	} else {
		AlertWindow::showMessageBoxAsync(AlertWindow::WarningIcon, "Error", "Could not write to file.");
} else {
	AlertWindow::showMessageBoxAsync(AlertWindow::WarningIcon, "Error", "Could not create file.");

At this point the file is created (passes f.create().ok()) but I can’t write into it, so it fails f.replaceWithText("Test OK!")

Actually, the file is created even if no File operation is done by the Juce code. Just selecting it from a FileChooser in saveMode creates the file. Also, f I keep trying over and over, every once in a while the file is written, but it’s not consistent.

Am I doing something wrong?

Just to continue my monologue, if I use the native file chooser, the file is created automatically even if I take no action with the resulting URI, but the native browser lets me navigate through all user directories. On the other hand, if I invoke Juce’s internal FileChooser, I can only navigate within the app’s sandbox and I can write, read, delete files, even though the “Create new folder” button of the file chooser can’t be used because the touch keyboard doesn’t pop up, so you can’t type the name of your new folder. Anyway, looks like the URI returned by the native browser isn’t properly translated into a file:// path.

Just found a pattern… When I create the file chooser object of native type, I pass a starting path and file name, if I use that, the file is created and written, but if I change the file name o try to write in a different path, the file is created but not written.

fileChooser.reset(new FileChooser("Select file", File(defaultFileName), "*.txt", true));

We’ve now added support for the new storage APIs, which are required when targeting android 29+:

Unfortunately, it seems that certain locations (especially shared locations) can no longer be accessed via a file path. Instead, files at these locations must be accessed via the ContentResolver mechanism, using a content:// URL returned from a native file chooser.

1 Like

Thanks for replying to this old post. I’ll try the new class as soon as I return on that (defective) Android App.