how8570
how8570

Reputation: 23

How to trash an image which on SD card via MediaStore#createTrashRequest?

12/17 Update:

This is a bug, and it seems a fixed version will come out soon.

And also a workaround provide here for older version, see: https://issuetracker.google.com/issues/350540990#comment7


I'm tring implment a trash feature on my app, which able let user trash/restore the files via this app.

So I wrote something like this, create trash intent via MediaStore#createTrashRequest, launching with ActivityResultLauncher:

val launcher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
  // ...
}

// this uri from cursor query, hard code here just for the example
val uri = Uri.parse("content://media/external/images/media/1234") 

val deleteIntent = MediaStore.createTrashRequest(contentResolver, listOf(uri), true)

launcher.launch(
    IntentSenderRequest.Builder(deleteIntent.intentSender).build()
)

And here is my AndroidManifest permissions:

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_MEDIA" />

than this dialog shows up, click the "allow"

Android 11 MediaStore op Dialog

For those files on Device, it work perfectly. Item on device been rename as .trashed_<expire_timestamp>_<original_Filename>, And the MediaStore IS_TRASHED column also been mark to 1. MediaStore generation increase.

However, for those who in SD card authought the system dialog shows up, clicking allow won't change anything. The file name in SD card still same, MediaStore record IS_TRASHED still 0, and no any exception been thrown. But MediaStore generation Do increase, seems weird...

Dose there any thing i missed?

Upvotes: 0

Views: 62

Answers (1)

Source30
Source30

Reputation: 495

To move an item on an SD card to the trash, you need the correct URI

The URI content://media/external/images/media/1234 refers to the "external_primary" storage.
For an item on the SD card, the URI should correspond to the specific volume ID, such as:
content://media/5216-4f19/images/media/1234.

Upvotes: 0

Related Questions