How to fetch pdf uri using MediaStore API in api level 30?
Asked Answered
P

0

6

I am using media-store for listing pdf in recycler view. Media store does not load non-media files on android 10 and above but works for media files

In build.gradle

  compileSdkVersion 30
  buildToolsVersion "30.0.2"
  minSdkVersion 21
  targetSdkVersion 30

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)
                        arrayList.add(Pdf(name, fileUri.toString(), data))
    
                    } while (cursor.moveToNext())
                }
                cursor.close()
            }
            return arrayList
        }
    }

In Manifest:

I have only READ_EXTERNAL_STORAGE permission

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 still not working

Any help would be appreciated.

Proud answered 13/8, 2021 at 11:56 Comment(3)
Remove the .DATA column.Avaunt
Have you considered that it might be an issue with the emulator? Is it having the same issue in a different device or emulator running 10?Close
@AbishekStephen yes, I have tried on different devices other than emulator same issue.Proud

© 2022 - 2024 — McMap. All rights reserved.