Strange file behaviors in Android and iOS

I have this code that is using FileChooser to save a (audio) file. It works fine in windows, macOS and Linux, but not in Android (and iOS):

	File fileToSave = lastRecording;
	lastRecording = File(); // "Close" fille

	juce::File initialFilAndDirectory;

	initialFilAndDirectory =
		File::getSpecialLocation
		(
			File::userMusicDirectory
		).getChildFile
		(
			fileToSave.getFileName()
		);

	myChooser =
		std::make_unique <FileChooser>
		(
			"Save Audio File as..."
			,
			initialFilAndDirectory
			,
			"*.wav"
		);


	myChooser->launchAsync
	(
		FileBrowserComponent::saveMode
		|
		FileBrowserComponent::canSelectFiles
		|
		FileBrowserComponent::warnAboutOverwriting
		,
		[fileToSave](const FileChooser& chooser)
		{
			auto rslt = chooser.getResult();
			if (rslt == File{})
				return;

			File soundFile(rslt);
			fileToSave.moveFileTo(soundFile);
		}
	);

On Android (and iOS) the file is created fine with
myChooser->launchAsync
but when the
fileToSave.moveFileTo(soundFile);

is run it returns false at

#if ! NAMES_ARE_CASE_SENSITIVE
if (*this != newFile)
#endif
if (! newFile.deleteFile())
return false;

and if I run

soundFile.hasWriteAccess();

it always returns false

How can I solve that problem ?


Eigil

Howdy

On mobile you need to use URI to copy/move file.
https://docs.juce.com/master/classFileChooser.html#details

Thank you very much for the reply!


Eigil

Thanks for the reply!

I can’t really find out how to do it on Android.

Do you have an example that I can look at ?

If I run DemoRunner in Android studio and select
Browse Demos → GUI → DialogsDemo.h → “Use Native Windows” → ‘Save’ File Browser → Downloads Then I end in jassertfalse in

getCursorDataColumn (in juce_android_Files.cpp):

            if (jniCheckHasExceptionOccurredAndClear())
            {
                // An exception has occurred, have you acquired RuntimePermission::readExternalStorage permission?
                jassertfalse;
                return {};
            }

BUT the file is written OK

If instead I select SDCARD->Download as the destination, then the save seems to go well, and afterwards the fie is created ok, but has 0 bytes

???

(I have no physical Android device, only Emulators.)


Eigil

It seems that changing to JUCE 7 and using the code from DialogsDemo.h solves the problem. !

1 Like