Read file from Documents directory on Android

Viewed 47

I have been trying to figure out how to deal with Android storage for a while now, and I have trouble finding a complete and up-to-date answer to what seems like a simple need:

  1. I want to list the files in a subfolder of the Documents directory.
  2. Next, I just want to read one of these files from the Documents directory. Simply reading it into a byte[]

Note: I do not want the user to pick files! For more information: I have 2 separate apps. I want app B to access files saved by app A. So I can share some settings and such.

Now, with the whole complicated permission and storage system, I find myself unable to do this simple task...

I am using Flutter with Dart, but I would be happy to know how to do it in just Java as well.

As I understand, SAF is only useful if you want to present your user a file picker. I do not want this. I also do not want to "share" the files like you would let the user share files between apps (like sharing a photo to the Discord app).

I simply want app B to access a file I saved from app A into the Documents directory. And list the available files to know what is there.

I have seen many people asking things like this, but I cannot find a clear, complete answer that does not simply tell me to enable full file access permission, which the store seems to really dislike, so I want to avoid it. Or the answer to just enable requestLegacyExternalStorage in the manifest, this does not really work, and as I understand it, it might not be useful after Android 31 or so. I want something clear, robust, and applicable to any version of Android after 22 or so.

Sidenote: I am perfectly allowed to save the file. Apparently Android does not care about this...

I have tried enabling external storage permission, media storage permission, requestLegacyExternalStorage, without success.

I also tried using full external storage file access, which technically works, but is not a good solution because I need to force the user to the permission settings screen, and the store will probably not allow it.

I can get the path perfectly fine, and I can save files perfectly fine.

If I am not mistaken, I can even read a file if it was saved from the same app. So if the app first saves a file in this location, it does not complain at all and will open it without needing any permissions. Only when the file was not made by the same app, does it cause problems. And when I uninstall the app, and then rebuild, it will also no longer load the files created by the previous build. Unless I am mistaken

1 Answers

Since Android 11: App B cannot simply read the files app A places in a subfolder of Documents directory.

App B can try to list the files in the subfolder but will see only files created by the app itself. Not of other apps. Not of app A.

You can use SAF ACTION_OPEN_DOCUMENT_TREE to let the user of app B pick the subfolder.

After that app B can list all the files in that folder and has access to all files.

If you do not want a user interaction for app B then extend ContentProvider in app A. App A can then serve its files to B.

Related