Content Uri for specific folder

553 Views Asked by At

I am new to android development and trying to build a WhatsApp status saver app. For SDK level 29 and above we need content resolver to access the files in a specific folder.

Here is my childDocumentUri Uri to pass into contentResolver.query(..) method:

val wa_status_uri = Uri.parse("content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fmedia/document/primary%3AAndroid%2Fmedia%2Fcom.whatsapp%2FWhatsApp%2FMedia%2F.Statuses")

val childDocumentUri = DocumentsContract.buildChildDocumentsUriUsingTree(wa_status_uri, DocumentsContract.getDocumentId(wa_status_uri))

This is how I'm trying to access all the files:-

fun getImageStatuses(childDocumentUri: Uri): MutableList<Status>{
        val projection = arrayOf(
            MediaStore.Files.FileColumns._ID,
            MediaStore.Files.FileColumns.DISPLAY_NAME,
            MediaStore.Files.FileColumns.MIME_TYPE
        )

        val selection = "${MediaStore.Files.FileColumns.MEDIA_TYPE} LIKE ?"
        val selectionArgs = arrayOf("image/jpeg")

        val statusMutableList = mutableListOf<Status>()
        contentResolver.query(
            childDocumentUri,
            projection,
            selection,
            selectionArgs,
            "${MediaStore.Files.FileColumns.DATE_ADDED} DESC"
        )?.use { cursor ->

            val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID)
            val nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME)
            val mimeColumn = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE)

            while (cursor.moveToNext()) {
                val id = cursor.getLong(idColumn)
                val displayName = cursor.getString(nameColumn)
                val mimeType = cursor.getString(mimeColumn)
                val contentUri = ContentUris.withAppendedId(
                    collection,
                    id
                )

                if (selectionArgs == null || mimeType == selectionArgs[0]) {
                    statusMutableList.add(Status(id, displayName, contentUri))
                }

            }
            statusMutableList
        } ?: mutableListOf()

        return statusMutableList
}

The problem is that instead of returning just jpg file because of selection and selectionArgs, cursor is returning mp4 files also and I've to explicitly filter images like this:-

if (selectionArgs == null || mimeType == selectionArgs[0]) {
                    statusMutableList.add(Status(id, displayName, contentUri))
                }

I've already taken persistable uri permission as shown in this answer but it's only for sdk level 29 and up. How can I do the same for sdk level 28 and below. Can I even use this contentResolver.query(..) method in sdk level 28 and below. If yes, then what will be my collection Uri and how will I take persistable permission for WhatsApp folder located as: Android/WhatsApp/

1

There are 1 best solutions below

0
blackapps On

val selection = "${MediaStore.Files.FileColumns.MEDIA_TYPE} LIKE ?"

val selection = "${MediaStore.Files.FileColumns.MIME_TYPE} LIKE ?"