Home > Blockchain >  How to request permissions on Android
How to request permissions on Android

Time:01-28

I am very new to Android programming and I am having trouble requesting storage permission. The idea is to have the phone generate a small label that will print from a mobile printer via Bluetooth, but as proof of concept I was going to have the phone just save a PDF or something for now.

I added this line to the AndroidManifest.xml:

    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:remove="android:maxSDKVersion" />

and from the MainActivity.kt:

class MainActivity : AppCompatActivity() {

    private val REQUEST_STORAGE = 101

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED)
        {
            println("Permissions Denied")
            requestStoragePermission()
            println("Passed Command")
        } else {
            println("PERMISSIONS GRANTED")
        }

    private fun requestStoragePermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(
                this,
                permission.WRITE_EXTERNAL_STORAGE
            )
        ) {
            requestPermissions(this, arrayOf(permission.WRITE_EXTERNAL_STORAGE), REQUEST_STORAGE)
        } else {
            // Eh, prompt anyway
            requestPermissions(this, arrayOf(permission.WRITE_EXTERNAL_STORAGE), REQUEST_STORAGE)
        }
    }
}

No matter what iteration of the requestPermissions command I try, the dialog box never shows and attempting to save any file results in a failure. I know this question has been asked a lot, but none of the solutions that work for other people are working here and I'm not sure why.

I have tried:

\\this is latest iteration
requestPermissions(this, arrayOf(permission.WRITE_EXTERNAL_STORAGE), REQUEST_STORAGE)

\\this was first iteration
ActivityCompact.requestPermissions(
    this@MainActivity,
    arrayOf(permission.WRITE_EXTERNAL_SOTRAGE),
    REQUEST_STORAGE
)

\\this one also caused an error so I abandoned the idea of moving this out of the main class
ActivityCompact.requestPermissions(
    MainActivity(),
    arrayOf(permission.WRITE_EXTERNAL_SOTRAGE),
    REQUEST_STORAGE
)

I need the user to be able to give storage access, at least while it's still in development to convince my boss to buy a mobile printer I can use to print the actual label.

CodePudding user response:

I have started default google sample project related to request runtime permissions topic. By default it uses camera permission. When I had changed Camera permission to WRITE_EXTERNAL_STORAGE permission it stops showing permission dialog too. It means nothing is wrong with your code, the issue has some different cause.

Likely it is related to changes in behaviour related to new Android API.

I haven't found root cause yet, but this links shows changes in behaviour on Android 9, 10, 11:

UPDATE Code above doesn't show dialog for MANAGE_EXTERNAL_STORAGE permission, but you have asked for WRITE_EXTERNAL_STORAGE permission. Default google sample is able to show this permission dialog on Android API 30. Here is relevant code:

        if (shouldShowRequestPermissionRationaleCompat(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // Display a SnackBar with a button to request the missing permission.
            layout.showSnackbar(R.string.camera_access_required,
                    Snackbar.LENGTH_INDEFINITE, R.string.ok) {
                requestPermissionsCompat(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                        PERMISSION_REQUEST_CAMERA)
            }

        } else {
            layout.showSnackbar(R.string.camera_permission_not_available, Snackbar.LENGTH_SHORT)

            // Request the permission. The result will be received in onRequestPermissionResult().
            requestPermissionsCompat(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), PERMISSION_REQUEST_CAMERA)
        }
fun AppCompatActivity.requestPermissionsCompat(permissionsArray: Array<String>,
        requestCode: Int) {
    ActivityCompat.requestPermissions(this, permissionsArray, requestCode)
}
  • Related