apache / cordova

Apache Cordova
https://cordova.apache.org/
584 stars 61 forks source link

Feature Request(Android): MediaStore API #334

Open customautosys opened 1 year ago

customautosys commented 1 year ago

Hi, I was using cordova-plugin-file, but now with scoped storage it is not practical if I want to allow the user to choose where they want to save the file.

The alternative is MediaStore API, however, https://github.com/Heartade/cordova-plugin-android-mediastore allows only saving a single image to the gallery and https://github.com/a-ferrari/com-thesis-cordova-plugins-pdfstore only a single PDF file.

This should be part of core functionality as many apps need to download files to a location selected by the user.

breautek commented 1 year ago

but now with scoped storage it is not practical if I want to allow the user to choose where they want to save the file.

Why isn't it practical? Scoped storage did add some restrictions, such as making the external root directory (either sdcard or emulated) an non-writable path, but apps can still write to the predefined folders such as external Pictures, Audio, etc.

You can also read files from external storage that was written by the app. Reading files written by other apps still requires the READ_EXTERNAL_STORAGE permission, which cordova-plugin-file handles.

customautosys commented 1 year ago

but now with scoped storage it is not practical if I want to allow the user to choose where they want to save the file.

Why isn't it practical? Scoped storage did add some restrictions, such as making the external root directory (either sdcard or emulated) an non-writable path, but apps can still write to the predefined folders such as external Pictures, Audio, etc.

You can also read files from external storage that was written by the app. Reading files written by other apps still requires the READ_EXTERNAL_STORAGE permission, which cordova-plugin-file handles.

I created a new plugin simply because that's faster to do, https://github.com/customautosys/cordova-plugin-saf-mediastore

I think we should consider making Mediastore / SAF a core part of Cordova Android. It really is the way forward for file saving on Android.

breautek commented 1 year ago

It really is the way forward for file saving on Android.

That's not entirely accurate. Scoped Storage (And the MediaStore APIs) are for dealing with files that uses the External storage system on android. a Media Store plugin could be used in addition to the file plugin, but it definitely won't replace the file plugin, which can interact with both the internal filesystem as well as the external filesystem. Currently the file plugin handles this through the Direct File API. But I do understand the MediaStore is the recommended approach for interfacing with the external filesystem.

To be clear, I'm not pushing back on the MediaStore API. I'm just asking the what it provides over the File plugin or why it's not practical. For example, how were you providing UI to the user to choose where to save a file before and why doesn't that work now?

customautosys commented 1 year ago

It really is the way forward for file saving on Android.

That's not entirely accurate. Scoped Storage (And the MediaStore APIs) are for dealing with files that uses the External storage system on android. a Media Store plugin could be used in addition to the file plugin, but it definitely won't replace the file plugin, which can interact with both the internal filesystem as well as the external filesystem. Currently the file plugin handles this through the Direct File API. But I do understand the MediaStore is the recommended approach for interfacing with the external filesystem.

To be clear, I'm not pushing back on the MediaStore API. I'm just asking the what it provides over the File plugin or why it's not practical. For example, how were you providing UI to the user to choose where to save a file before and why doesn't that work now?

I was using https://github.com/ourcodeworld/cordova-ourcodeworld-filebrowser which does not display the files in the folders disallowed by scoped storage. When you navigate to such a folder then you can't go anywhere and you're stuck and have to close the picker. That's why I had to write the MediaStore / SAF plugin.

File plugin also fails on a scoped storage device whenever you write to something that's not the app's own folder, whether in internal or external storage. So it ends up being pretty limited.

breautek commented 1 year ago

To addon...

Starting with API 33, READ_EXTERNAL_STORAGE permission is going to change and be no-op, with 3 more specific read permissions replacing it, for audio, video and images. Which I think may further promote a need for an actual MediaStore API.