1

I am trying to access a PDF file from external storage, but the request for runtime permission to access the PDF file is not working on Android API level 33 (Android 13). Could you please provide a solution that works across all Android API levels?

My Code Please check this code; the permission dialog is not popping up in Android 13 (API level 33).

//all variable and function related to take image and pdf from gallery
private val REQUEST_PERMISSIONS_CODE: Int = 123
private val getContent =
    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            val data: Intent? = result.data
            if (data != null) {
                if (lType ==1){
                    uriPdf = data.data!!
                    val path = getRealPathFromURI(uriPdf)
                    binding.imgGstName.setText(path.trim().substring(path.lastIndexOf("/") + 1))
                }else if (lType ==2){
                    uriCancledCheque = data.data!!
                    uriAadharCard = data.data!!
                    uriProfile = data.data!!
                    val path = getRealPathFromURI(uriCancledCheque)
                    binding.imgCancelledChequeName.setText(path.trim().substring(path.lastIndexOf("/") + 1))
                }
            }
        }
    }
private fun getRealPathFromURI(uri: Uri?): String {
    val projection = arrayOf(MediaStore.Images.Media.DATA)
    val cursor = contentResolver.query(uri!!, projection, null, null, null)
    val columnIndex = cursor?.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
    cursor?.moveToFirst()
    val path = columnIndex?.let { cursor.getString(it) }
    cursor?.close()
    return path ?: ""
}
private val requestPermissionLauncher =
    registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()){permissions->
        val granted = permissions.entries.all {
            it.value == true
        }
        if (granted){
            //If the use grants the permissions , proceed with your code
            if (lType == 1){
                val intent = Intent(Intent.ACTION_GET_CONTENT)
                intent.type = "application/pdf"
                getContent.launch(intent)
            }else if (lType == 2){
                val intent = Intent(Intent.ACTION_PICK)
                intent.type = "image/*"
                getContent.launch(intent)
            }
        }else{
            //If the user denies the permissions, handle the error gracefylly
        }
    }


    binding.uploadGstCertificate.setOnClickListener {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
            || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
            lType = 1
            val intent = Intent(Intent.ACTION_GET_CONTENT)
            intent.type = "application/pdf"
            getContent.launch(intent)
        }else{
            requestPermissionLauncher.launch(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE))
        }
    }

    binding.uploadCancelledCheque.setOnClickListener {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
            || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
            lType = 2
            val intent = Intent(Intent.ACTION_PICK)
            intent.type = "image/*"
            getContent.launch(intent)
        }else{
            requestPermissionLauncher.launch(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE))
        }
    }
}
  • 1
    Which pdf permission are you talking about? And where is your code? We dont know what you do. – blackapps May 12 '23 at 07:24
  • Can you first TELL which permissions you are asking? And after that the code doing so. – blackapps May 12 '23 at 15:02
  • @blackapps I want to read PDF and image files from external storage, for which I use the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions. After that, I make a network call to upload the PDF and image files to the server. If I am unable to explain my problem to you, you can provide me with your code to read PDF and image files from external storage in android 13(Api Level 33) and upload them to the server. – Waseem Idrisi May 13 '23 at 05:27
  • For a 33 device you should not ask fot READ and WRITE permission anymore. Tell further what goes wrong. – blackapps May 13 '23 at 05:53
  • My question is about the fact that in Android 13, there are only three permissions for accessing the External Storage: READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, and READ_MEDIA_AUDIO. How can I request permission to access PDF and Other Files in Android 13? You can see [link](https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions) Behavior changes. – Waseem Idrisi May 30 '23 at 12:22
  • You do not need any permission as long as your app created those files itself. – blackapps May 30 '23 at 12:24
  • If the files do not belong to your app you have to use SAF to select them. I.e. ACTION_GET_CONTENT. You never need any permission using ACTION_GET_CONTENT. In not one Android version. You can select any file with it and read it. – blackapps May 30 '23 at 12:24
  • Please can you provide the sample code to access pdf file, if the file do not belong to my app? – Waseem Idrisi May 30 '23 at 12:35
  • Well what goes wrong? I see you already use ACTION_GET_CONTENT and that you obtain a nice uri. You can use the uri to open an InputStream. You have never told what went wrong. You only complained about permissions. But you do not need any permission. Better remove all permission code and focus on the obtained uri. – blackapps May 30 '23 at 12:54
  • `I want to read PDF and image files from external storage,` No. You have nothing to do with external storage if you use ACTION_GET_CONTENT. You will get an uri. That uri can be from Google Drive and Dropbox too. Its all irrelevant where the uri comes from. Just use the uri. – blackapps May 30 '23 at 13:00

0 Answers0