Waseem Idrisi
Waseem Idrisi

Reputation: 31

How to request runtime permission for PDF in android 13(API level 33)?

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))
        }
    }
}

Upvotes: 3

Views: 2159

Answers (0)

Related Questions