Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): add READ_MEDIA_* permissions for Android 13+ #566

Conversation

MauriceFrank
Copy link
Contributor

@breautek breautek added this to the 8.x milestone Apr 28, 2023
@breautek
Copy link
Contributor

I enabled test but they will likely fail on Android because it will depend on the platform using Compile SDK 33. The Android should be resolved when cordova-android@12 is released.

@MauriceFrank
Copy link
Contributor Author

I enabled test but they will likely fail on Android because it will depend on the platform using Compile SDK 33. The Android should be resolved when cordova-android@12 is released.

Actually I just retested and it works fine while just requesting one permission at a time (READ_MEDIA_IMAGES), but it fails reading (file.listDir() with folder "DCIM") when trying to request all at once. Might be a syntax mistake in the PR or another issue.

Shall I update it or close it? For my own usage, it is enough to request the READ_MEDIA_IMAGES. However, for other users it might not be enough, since it should work with all types of Media. That's why I have tried with this PR to request all needed permissions but obviously it isn't as simple.

As you suggested, the better approach for accessing external storage would be to not use the file-plugin at all anymore and rather use (if existing) a cordova plugin using the MediaStore API.

Copy link
Contributor

@breautek breautek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes looks fine to me, even if it's just a stopgap...

Removing support for reading external storage altogether is a significant change that will require an official vote. And I think before we can explore that route, we'll need a MediaStore API alternative that can be used, even if it's a third-party plugin.

@erisu erisu changed the title Add READ_MEDIA - permissions for Android 13+ feat(android): add READ_MEDIA_* permissions for Android 13+ Jul 4, 2023
Copy link
Member

@erisu erisu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ferdianarief
Copy link

So, how about permission to read document like pdf, world and xls from external storage (emulated/0/Download/.....)

i tried copyTo but still error ( error code : 1)

@breautek
Copy link
Contributor

breautek commented Apr 28, 2024

So, how about permission to read document like pdf, world and xls from external storage (emulated/0/Download/.....)

i tried copyTo but still error ( error code : 1)

Android has no permission for documents, and by extension doesn't support documents via the file API while working with external storage. Instead you have to use a plugin that interfaces with the native MediaStore API to work with document files. More info on this is noted here.

Edit: Extending on this comment, using the media store for accessing the external storage (aka anything inside /storage/emulated/ or /sdcard) is the way to go, even for media files as Google will be enforcing restrictions on the READ_MEDIA_* permission usage to only those apps who actually require broad access to those media assets. This means using these permissions is OK if your app requires being able to view many of these assets (an image gallery app for example) but if you require infrequent access to a limited set of assets, then you might be forced by Google to use the media store route to get access to specific resources.

@ferdianarief
Copy link

thanks @breautek for quick response,

Screen Shot 2024-05-01 at 16 43 31

copying file is unseccessful, and this is my code

window.resolveLocalFileSystemURL(docPath,function (fileEntry)
{
console.log(JSON.stringify(fileEntry));
window.resolveLocalFileSystemURL(cordova.file.dataDirectory,function (directory)
{
console.log(JSON.stringify(directory))
fileEntry.moveTo(directory, newFileName,function(){
alert('Successful Copy!');
},
function()
{
alert('Copying Unsuccessful ');
});
},null);
}, null);

@breautek
Copy link
Contributor

breautek commented May 1, 2024

copying file is unseccessful, and this is my code

Because you're working with a document file, you can't use a filesystem API. You can use filesystem API for internal storage, but not with files on external storage. It looks like you're attempting to move a PDF document from external storage into internal storage. It will error on reading a document from the external storage path (the /storage/emulated/0/Documents/ path). This is a limitation of Android's Scoped Storage model, which is enforced on API 29 and later.

Generally speaking interacting with files on external storage requires using a media storage API, which this plugin does not implement. See here for a list of plugins that implements the media store API.

I'm locking this thread because an old merged PR is not really a place to ask questions or to provide support. If you have further questions, please use Discussions instead.

@apache apache locked as resolved and limited conversation to collaborators May 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants