Scoped Storage & MediaStore & Api 30: Can delete track from Playlist, but can't add

Viewed 295

I have a music player app for Android. There is a strange issue with MediaStore. I can delete or add songs to my own playlists on MediaStore (eg, created by my app). However, when I try to add a song to a playlist created by another application, it throws java.lang.SecurityException, but I can delete songs from the exact same playlist!

I have all required permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

And my code:

public Uri addTrackToPlaylist(Context context, TrackLocal trackLocal, long playlist_id, int pos) {
    ContentResolver resolver = context.getContentResolver();
    Uri newuri = MediaStore.Audio.Playlists.Members.getContentUri(getVolume(), playlist_id);
    ContentValues values = new ContentValues();
    values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, 0);
    values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, trackLocal.dbid);
    Uri uri = resolver.insert(newuri, values);
    return uri;
}

public int deletePlaylistTrack(Context context, long playlistId, TrackLocal trackLocal) {
    ContentResolver resolver = context.getContentResolver();
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri(getVolume(), playlistId);
    String filter = MediaStore.Audio.Playlists.Members.AUDIO_ID + " = " + trackLocal.dbid;
    return resolver.delete(uri, filter, null);
}

private String getVolume() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        return MediaStore.VOLUME_EXTERNAL;
    } else {
        return "external";
    }
}

Does anyone have an idea at least how to workaround this?

Thanks.

0 Answers
Related