Saturday 21 August 2021

How to fetch pdf uri using MediaStore API in api level 29?

I am using media-store for listing pdf in recycler view. it works fine till android 10 and also works in android 11 but only not working in android 10.

I have also set requestLegacyExternalStorage=true in manifest.

In build.gradle

  compileSdkVersion 29
  buildToolsVersion "30.0.2"
  minSdkVersion 21
  targetSdkVersion 29

FetchPdf.kt

 class FetchPdf(private val applicationContext: Context) {
        private val arrayList = ArrayList<Pdf>()
    
        fun getPdf(): ArrayList<Pdf> {
    
            val collection = MediaStore.Files.getContentUri("external")
            val projection = arrayOf(
                    MediaStore.Files.FileColumns._ID,
                    MediaStore.Files.FileColumns.DATE_MODIFIED,
                    MediaStore.Files.FileColumns.DISPLAY_NAME,        
                    MediaStore.Files.FileColumns.SIZE,
                    MediaStore.Files.FileColumns.DATA
            )
    
            val mimeType = "application/pdf"
    
            val whereClause = MediaStore.Files.FileColumns.MIME_TYPE + " IN ('" + mimeType + "')"
            val orderBy = MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC"
            val cursor: Cursor? = applicationContext.contentResolver.query(
                    collection,
                    projection,
                    whereClause,
                    null,
                    orderBy
            )
            if (cursor != null) {
                val idCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID)
               val nameCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME)
                val sizeCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE)
                val dataCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA)
                if (cursor.moveToFirst()) {
                    do {
                        val fileUri: Uri = Uri.withAppendedPath(
                                MediaStore.Files.getContentUri("external"),
                                cursor.getString(idCol)
                        )
                        val name = cursor.getString(nameCol)
                        val data = cursor.getString(dataCol)
                        val size = cursor.getLong(sizeCol)
                        if (size == 0L) continue
                        arrayList.add(Pdf(name, fileUri.toString(), data))
    
                    } while (cursor.moveToNext())
                }
                cursor.close()
            }
            return arrayList
        }
    }

Problem only occurring in android 10 .working fine in android 11

cursor.moveToFirst() is returning false

It is returning arrayList size 0

What I have tried:

I tried changing MediaStore.Files.FileColumns.DATA to MediaStore.Files.FileColumns.RELATIVE_PATH not working

I tried traversing each and every folder recursively then its working but i want MediaStore solution



from How to fetch pdf uri using MediaStore API in api level 29?

No comments:

Post a Comment