I’m developing an Android application and need to retrieve images that have been deleted and moved to the trash bin. I’ve attempted to use MediaStore.Images.Media.IS_TRASHED as a projection, but I haven’t been able to retrieve any deleted images.
Here is the code snippet I am using:
fun queryAllImages() {
val files = arrayListOf<MediaModel>()
ctx.contentResolver.query(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
} else {
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
}, arrayOf(
imageData,
imageId,
imageDisplayName,
imageSize,
imageWidth,
imageHeight,
imageDateModified,
imageIsTrash
), if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
"$imageIsTrash = 0 OR $imageIsTrash = 1"
} else {
null
}, null, null
).use { cursor ->
val dataColumn = cursor?.getColumnIndexOrThrow(imageData)
val nameColumn = cursor?.getColumnIndexOrThrow(imageDisplayName)
val isTrashColumn = cursor?.getColumnIndexOrThrow(imageIsTrash)
var path: String? = null
var name: String? = null
var isTrash: Boolean? = null
if (cursor != null) {
while (cursor.moveToNext()) {
path = dataColumn?.let { cursor.getString(it) }
name = nameColumn?.let { cursor.getString(it) }
isTrash = isTrashColumn?.let { cursor.getInt(it) } == 1
Log.i(TAG, "queryImages: $path n $isTrash")
}
}
}
}