Exception trying to create delete request via MediaStore.createDeleteRequest(getContentResolver(), uris) for Android R (API 30) for scoped storage

Viewed 1246

Trying to target Android R (API 30) using scoped storage. I get the image picker to open the media library to allow user to select 1 or more images using:

new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

I get the list of Uris e.g.

content://com.android.providers.media.documents/document/image%3A189
content://com.android.providers.media.documents/document/image%3A190

I then attempt to generate the intent to ask permission to delete:

PendingIntent intent = MediaStore.createDeleteRequest(getContentResolver(), uris);

But this throws the exception:

java.lang.IllegalArgumentException: All requested items must be referenced by specific ID
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
    at android.content.ContentProviderProxy.call(ContentProviderNative.java:732)
    at android.content.ContentResolver.call(ContentResolver.java:2405)
    at android.provider.MediaStore.createRequest(MediaStore.java:822)
    at android.provider.MediaStore.createDeleteRequest(MediaStore.java:985)
    ...

How is one to convert the Uris into the special format expected by MediaStore.createDeleteRequest?

1 Answers

In the end I re-generated the URI by extracting the media number from the returned URI and then using:

ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Long.parseLong(num));

to create the URI expected by the MediaStore API. It seems strange that the image picker does not return the URI expected by other media services.

Related