How to get media list from trash bin in kotlin

174 Views Asked by At

"I am developing a photo gallery app for Android 11 and later users, where when a user deletes any photo or video, I move it to a trash folder instead of permanently deleting it.

 val selectedMedia = getSelectedMedia()
            val uriList = ArrayList<Uri>()
            selectedMedia.forEach {
                uriList.add(getUri(it))
            }
           

            try {
                val pendingIntent =
                    MediaStore.createTrashRequest(activity.contentResolver, uriList, true)

                // Launch a system prompt requesting user permission for the operation.
                startIntentSenderForResult(
                    activity,
                    pendingIntent.intentSender,
                    101,
                    null,
                    0,
                    0,
                    0,
                    null
                )
            } catch (e: Exception) {
                e.printStackTrace()
            }

"I have successfully implemented the feature to move selected media to the trash. However, I now want to display all the media items that are inside the trash so that users can restore them. I am aware that this functionality can be achieved by using Google's apps like Gallery, Google Photos, or File Manager. Nevertheless, I'm looking to implement the same trash folder within my Gallery app. How can I accomplish this in Kotlin?"

I am trying to get hidden files using content resolver but it give nothing.

fun getHiddenMediaFiles(context: Context): List<String> {
    val hiddenMediaList = mutableListOf<String>()
    val contentResolver: ContentResolver = context.contentResolver
    val uri: Uri = MediaStore.Files.getContentUri("external")
    val projection = arrayOf(
        MediaStore.Files.FileColumns.DATA,
        MediaStore.Files.FileColumns.DISPLAY_NAME
    )

    val selection = "${MediaStore.Files.FileColumns.DATA} LIKE ?"
    val selectionArgs = arrayOf("%.trashed%")
    
    val cursor: Cursor? = contentResolver.query(
        uri,
        projection,
        selection,
        selectionArgs,
        null
    )

    cursor?.use {
        val dataColumnIndex = it.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA)
        while (it.moveToNext()) {
            val filePath = it.getString(dataColumnIndex)
            hiddenMediaList.add(filePath)
        }
    }
    cursor?.close()

    for (ab in hiddenMediaList){
        Log.e("data", "-----------size-----------------${hiddenMediaList.size}")
    }
    return hiddenMediaList
}
1

There are 1 best solutions below

0
Muhammad Adnan On

Actually we don't have media columns access that why we don't get this data. So hare is the method how we get trashed files.

val bundle = Bundle()
bundle.putInt("android:query-arg-match-trashed", 1)
bundle.putString(
    "android:query-arg-sql-selection",
    "${MediaStore.MediaColumns.IS_TRASHED} = 1"
)
bundle.putString(
    "android:query-arg-sql-sort-order",
    "${MediaStore.MediaColumns.DATE_MODIFIED} DESC"
)
val cursor = context.contentResolver.query(
    MediaStore.Files.getContentUri("external"),
    mediaProjection,
    bundle, null

)

you can use this. code to fetch trashed data. just add your mediaProjection and iterate over cursor.