1

I write this function to PDFfile form URI but it throw 2 exception with Android 10 11 12.

From Firebase Crashlytics

45% Xiaomi 14% samsung 12% oneplus 12% realme

38% Android 11 34% Android 12 19% Android 10 7% Android 9

1)

    java.lang.SecurityException: Permission Denial: reading com.whatsapp.contentprovider.MediaProvider uri content://com.whatsapp.provider.media/item/b1003092-8514-4d91-8600-3bbdb788c2bf from pid=21402, uid=10231 requires the provider be exported, or grantUriPermission()
    java.lang.SecurityException: myApp.scanner.docs.pdf has no access to content://media/external/downloads/65407

Funtion

   fun getPdfFileFromUri(activity: Context, pdfFileNames: Uri): File {
     val myFile: File
     var displayName: String? = null
     val uriString = pdfFileNames.toString()
     if (uriString.startsWith("content://")) {
        var cursor: Cursor? = null
 // following line responsible for crash 1
        cursor = activity.contentResolver.query(pdfFileNames, null, null, null, null)
        if (cursor != null && cursor.moveToFirst()) {
            displayName =
                cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
        }
        cursor?.close()
        if (displayName == null) {
            displayName = uriString.split("/")[uriString.split("/").size - 1]
        }
        myFile = File(activity.cacheDir, displayName)
        val output = FileOutputStream(myFile)
 // following line responsible for 2nd crash
        val asset = activity.contentResolver.openInputStream(pdfFileNames)
        val buffer = ByteArray(1024)
        var size: Int
        while (asset!!.read(buffer).also { size = it } != -1) {
            output.write(buffer, 0, size)
        }
        asset.close()
        output.close()
    } else {
        myFile = File(uriString)
    }
    return myFile
}
Laksh Lathiya
  • 322
  • 2
  • 12
  • Do you have define of file provider in manifest? https://developer.android.com/reference/androidx/core/content/FileProvider – Valery Boretsky Nov 10 '22 at 06:36
  • Can you first tell what that function should do? Can you then tell the value of the pdfFileNames parameter with which you call that function? We wanna see how many and which names you put in it. – blackapps Nov 10 '22 at 07:31
  • What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Nov 10 '22 at 07:57
  • @AlexMamo I make this function in utills and used it with 3 different activity. I'm facing issue when i open pdf file from fileManger via intent. also i want to inform that this issue doesn't happen every time. – Laksh Lathiya Nov 10 '22 at 08:12
  • @blackapps function fetch pdf file from local using URI. & function use single URI at a time. – Laksh Lathiya Nov 10 '22 at 08:46
  • So much code for fetching or reading a file? For that you only have to open an input stream and read from it. Three lines. Why do you have so much more code? And why didnt you put the used uri string in your code so we know what you do? Also explain what 'fetching' a file would be. Realise that the file is already on your device otherwise there was no uri. – blackapps Nov 10 '22 at 09:04

0 Answers0