0

In my app, I am saving files of different types of images, videos, pdf, etc. I am creating a directory if it does not already exist and saving the files generated by my app in that folder so that the user can easily find the files used and created by my app. But in Android 10 I'm unable to check and create directory in external storage.

I know that I can add android:requestLegacyExternalStorage="true" in my manifest to continue to use the old functionality but this is not the permanent solution. Also, I don't want to use getExternalFilesDir() as this folder created by this method will be deleted if the user uninstalls my app, and I want the files saved by the app to remain in his device so that those files can be used again.

This is the code I am using at present to check and create a directory of my app and store a file "myFile" in that directory.

File sd =Environment.getExternalStorageDirectory();
File directory = new File(sd.getAbsolutePath() + "/" + getResources().getString(R.string.app_name));

//create directory if not exist
if (!directory.isDirectory()) {
    directory.mkdirs();
}
File file = new File(directory, myFile);

How can I achieve the same result in Android 10? What I exactly want is:

  1. To check whether a directory with my app name exists or not. If the directory does not exist then create the directory in device external storage.
  2. Save all the files generated by my app in this directory.
  3. This folder should not get deleted even if the user uninstalls the app.
  4. Read files from this folder.
Edric
  • 24,639
  • 13
  • 81
  • 91
ANKIT
  • 87
  • 1
  • 8
  • If you use getFilesDIr(), getExternalFilesDir() or getExternalFIlesDirs() you app can make as many folders there as it wants in the classical way. – blackapps Oct 20 '19 at 09:43
  • 1
    If you do not want that change to modern times and use ACTION_OPEN_DOCUMENT_TREE to let the user pick internal memory or micro SD card. Once you have such a content tree scheme you can create as much folders as you want – blackapps Oct 20 '19 at 09:45
  • Folder generated by using getExternalFilesDir() or getFilesDir() will get deleted on app uninstall.I don't want folder to be removed. – ANKIT Oct 20 '19 at 09:51
  • Yes, i realised that already as you can see on my second comment. Please react on my second comment. – blackapps Oct 20 '19 at 11:43
  • I have not yet tried your second suggestion.I will let you know once I do that. – ANKIT Oct 20 '19 at 12:33

1 Answers1

0

In Android Q direct File access is disabled by default for the apps outside their private folders. You can use the method getPrimaryStorageVolume().createOpenDocumentTreeIntent() of class StorageManager to ask for access to the extenal primary volume. In this case you need user consent and you won't be able to use File api directly anyway, but using DocumentFile class you have a very similar interface, so this is the solution closer to the old behavior. It works if you need to perform operations in foreground and background, i.e. without user interaction with the exception the first interaction to request the permission. I link Flipper library, it helps to manage files like in older android versions.

greywolf82
  • 21,813
  • 18
  • 54
  • 108
  • `You can use the method getPrimaryStorageVolume().createOpenDocumentTreeIntent() of class StorageManager to ask for access to the extenal primary volume.`. Even if the user allows a specific directory you will get a silent cancel resultcode. So of no use sadly. – blackapps Oct 21 '19 at 08:03
  • @blackapps I use the library in my apps (which is based on that method) and it works as expected. – greywolf82 Oct 21 '19 at 09:59