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
toMediaStore.Files.FileColumns.RELATIVE_PATH
not working
I tried traversing each and every folder recursively still not working
Any help would be appreciated.