How to implement New Storage API in Android 11?
Asked Answered
P

3

7

Scenario

I've two apps, one is the Tracker App which records the incoming and outgoing calls and zipped these files, and sends the file path to the Main App via Inter-Process Communication which uploads these files to the server.

Now I'm upgrading both apps to Android 11. In Tracker App, I'm using MediaStore.Files API to save files and trying to read these files using the file path in Main App. While reading file File.canRead() returns false in Main App. Even I tried MediaStore API to read these files it returns empty Cursor.

Here I've few questions.

  1. Can I read files that are created by the Others app on Android 11? I read somewhere that you can't access others apps files in Android 11.
  2. Is my app eligible for MANAGE_EXTERNAL_STORAGE permission to access all files in storage?
  3. What will be the best way to handle this scenario?
  4. Can ``` Storage Access Framework `` help me to handle this scenario?

I'm saving files in the public directory Documents/AppData/Audio. Please give me working links regarding this. Any help will be appreciated. Thanks

Prang answered 22/5, 2021 at 2:35 Comment(7)
Read my question and answer here - https://mcmap.net/q/492453/-android-11-r-file-path-access/5550161Returnee
@HB I tried this already but it won't help me. Can I access files created by other apps in Documents folder?Prang
, I'm using MediaStore.Files API to save files and trying to read these files using the file path Wrong. You should use MediaStore api then too.Proceeds
Is my app eligible for MANAGE_EXTERNAL_STORAGE permission to access all files in storage? You should ask google.Proceeds
@Proceeds I'm also trying to access through MediaStore in selected folder it returns empty cursor in Main App. but when I try to access in Tracker App then cursor is not empty.Prang
In Tracker app you should query the media store and forwart the obtained uries with a file provider to Main app. Or with your own content/file provider. See answer.Proceeds
Answer with SAF implementation: https://mcmap.net/q/541284/-writing-many-files-on-android-11Vacillation
R
2

I've two apps, one is the Tracker App which records the incoming and outgoing calls and zipped these files, and sends the file path to the Main App via Inter-Process Communication which uploads these files to the server.

That seems overly complex.

Can I read files that are created by the Others app on Android 11?

Technically, you are not writing a file. You are creating an entry in MediaStore.Files. Other apps cannot read entries that you create in MediaStore.Files.

Is my app eligible for MANAGE_EXTERNAL_STORAGE permission to access all files in storage?

We are not Google. We have no way of answering that. It would surprise me greatly, though, if they considered your app to be eligible for this.

What will be the best way to handle this scenario?

Well, IMHO, the best way by far would be to have one app, not two. But I am assuming you are not in position to change that at this time.

If so, then:

  • Have the "tracker" app write the content to files in a filesystem directory that the app can write to. Mostly, that will be via methods on Context, such as getFilesDir() or getExternalCacheDir().

  • Have the "tracker" app use FileProvider to serve files from that directory, and use FileProvider.getUriForFile() to get a Uri pointing to that file.

  • Have the "tracker" app invoke your "main" app via some Intent-based mechanism (startActivity(), startService(), sendBroadcast(), etc.). Put the Uri from the previous bullet into that Intent, and add FLAG_GRANT_READ_URI_PERMISSION to that Intent.

  • Have the "main" app read the content using a ContentResolver and openInputStream(), passing in the Uri that it extracts from its copy of the Intent.

Can ``` Storage Access Framework `` help me to handle this scenario?

You could have the user, in each app, use the Storage Access Framework. For example, you could use ACTION_OPEN_DOCUMENT_TREE in each app, hoping that the user would choose the same tree in each app. Personally, I would use FileProvider, as it does not require user interaction and does not require the user to make good choices.

Rodriguez answered 22/5, 2021 at 13:31 Comment(1)
thanks for answering. as you suggested, I'm implementing SAF Framework for now. later I'll shift uploading task into "Tracker App".Prang
T
3

As in Android 11 MediaStore API only returns media files.

so, i will answer your questions related to it.

Can I read files that are created by the Others app on Android 11? I read somewhere that you can't access others apps files in Android 11.

No! you can't access the files created by other application that are stored in personal storage of that specific app.

Is my app eligible for MANAGE_EXTERNAL_STORAGE permission to access all files in storage?

As far as i understand, your app doesn't required external storage to store the data it can be also done in private storage where you can read or write your data. and if your app is eligible for this permission anyway still you can't use it as of now. it's been suggested by official web rather then asking this permission make target API 29 and use android:requestLegacyExternalStorage="true" in your manifest. click here to read about it.

What will be the best way to handle this scenario?

Rather using external audio path you can use you app specific folder to store the archive that you are creating.

Can ``` Storage Access Framework `` help me to handle this scenario?

I don't have much idea about how IPC work between two apps so i can't tell exactly that it will be better to use Storage framework.

Track answered 22/5, 2021 at 7:11 Comment(5)
only returns media files.. No one can put all kind of files in media store.Proceeds
earlier we can get non-media files like pdf, text, etc. like used in this.Track
Earlier? Now still too. What is it that you wanna say?Proceeds
sorry if my answer is wrong. but i am using a library called FilePicker link. in this library its uses MediaStore api to get non-media files. but after upgrading to API 30 it's not showing any non-media files.Track
I have two apps, one Tracker who save audio files in zip file and other is Main App who reads these file. So I can not use private storage to store.Prang
P
2

The receiving app can use SAF to let the user pick your directory.

Or more standard: you have files so you build your own file/content provider to serve your files.

And if you use inter process communication(how by the way) you could serve your files one by one using the uri from mediastore and FileProvider.

Proceeds answered 22/5, 2021 at 7:28 Comment(2)
can you please share working links to build own File/Content provider.?Prang
No i cannot. I have to google for that then so i let you do it.Proceeds
R
2

I've two apps, one is the Tracker App which records the incoming and outgoing calls and zipped these files, and sends the file path to the Main App via Inter-Process Communication which uploads these files to the server.

That seems overly complex.

Can I read files that are created by the Others app on Android 11?

Technically, you are not writing a file. You are creating an entry in MediaStore.Files. Other apps cannot read entries that you create in MediaStore.Files.

Is my app eligible for MANAGE_EXTERNAL_STORAGE permission to access all files in storage?

We are not Google. We have no way of answering that. It would surprise me greatly, though, if they considered your app to be eligible for this.

What will be the best way to handle this scenario?

Well, IMHO, the best way by far would be to have one app, not two. But I am assuming you are not in position to change that at this time.

If so, then:

  • Have the "tracker" app write the content to files in a filesystem directory that the app can write to. Mostly, that will be via methods on Context, such as getFilesDir() or getExternalCacheDir().

  • Have the "tracker" app use FileProvider to serve files from that directory, and use FileProvider.getUriForFile() to get a Uri pointing to that file.

  • Have the "tracker" app invoke your "main" app via some Intent-based mechanism (startActivity(), startService(), sendBroadcast(), etc.). Put the Uri from the previous bullet into that Intent, and add FLAG_GRANT_READ_URI_PERMISSION to that Intent.

  • Have the "main" app read the content using a ContentResolver and openInputStream(), passing in the Uri that it extracts from its copy of the Intent.

Can ``` Storage Access Framework `` help me to handle this scenario?

You could have the user, in each app, use the Storage Access Framework. For example, you could use ACTION_OPEN_DOCUMENT_TREE in each app, hoping that the user would choose the same tree in each app. Personally, I would use FileProvider, as it does not require user interaction and does not require the user to make good choices.

Rodriguez answered 22/5, 2021 at 13:31 Comment(1)
thanks for answering. as you suggested, I'm implementing SAF Framework for now. later I'll shift uploading task into "Tracker App".Prang

© 2022 - 2024 — McMap. All rights reserved.