Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API 29 Mediastore Access

My app creates playlists in the android mediastore. All is well for api's including 28 however, api 29 seems to require additional permissions. Inserting a new playlist name and id works without issue. When it comes to inserting track id and play order, an access permission exception is thrown. In verifying the Uri, i found that when resolver.insert for API 29 the exception error is:

java.lang.SecurityException: myapp_name has no access to content://media/external_primary/audio/media/146

The code:

Uri exturi = MediaStore.Audio.Playlists.Members.getContentUri("external", playlist_id);
// exturi : content://media/external/audio/playlists/227/members

// values : audio_id=146 play_order=0
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, play_order);
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audio_id);

try {
    resolver.insert(exturi, values);
} catch (Exception e) {
    e.printStackTrace();
}

Strange thing is that although inserting a new playlist into Mediastore works but adding tracks (track_id, play order) gives an access permission error

How to resolve this exception error for API 29?

Update Feb 2021: a small step forward, I am pretty sure I need to get Documenturi for the original uri but still gives me the access error. So the issue does not lie with accessing the tracks but with the uri itself.

doc_uri = MediaStore.getDocumentUri(context,playlist_members_uri);
java.lang.SecurityException: com.flyingdutchman.newplaylistmanager has no access to content://media/external/audio/playlists/130/members
like image 858
Theo Avatar asked Aug 07 '19 16:08

Theo


2 Answers

I think this is an Android 10 bug, so I've filed a report here: https://issuetracker.google.com/issues/147619577 (includes instructions for an emulator test case to reproduce it if that interests you). Please consider starring it to let the Android team know that it affects you.

From what I can tell, it only affects files on 'external' storage, like sdcards mounted on /storage/XXXX-XXXX

In the meantime, the only fix that some of my users were able to successfully apply is to move their music files to the internal storage (reboot and wait for the media scan to finish to be sure that MediaStore is up-to-date).

like image 85
now Avatar answered Sep 20 '22 02:09

now


in my further research for the answer, I came across this;

All about the media database (Mediastore) with android 11

like image 21
Theo Avatar answered Sep 19 '22 02:09

Theo