Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission Request (WRITE_EXTERNAL_STORAGE) doesn't show up

Android 6

I wrote the next code that checks for permissions and if there are no such permissions, it asks a user to give them.

private void checkDiskPermission ()
{
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(this, "no disk access" , Toast.LENGTH_LONG).show();
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }
    else
    {
        Toast.makeText(this, "disk access - OK" , Toast.LENGTH_LONG).show();
    }

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(this, "no GPS access" , Toast.LENGTH_LONG).show();
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0);
    }
    else
    {
        Toast.makeText(this, "GPS access - OK" , Toast.LENGTH_LONG).show();
    }
}

This code works fine for GPS permissions, but not for WRITE_EXTERNAL_STORAGE permissions. A dialog appears in only one case.

Why can it be so?

Thanks!

like image 936
yurart Avatar asked Nov 18 '25 03:11

yurart


2 Answers

Because you are requesting it for 2 times simultaneously. That's why it is taking last request.

The solution is that you have to ask both permission in one request

Like this-

private void checkDiskPermission ()
{
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(this, "No Permissions" , Toast.LENGTH_LONG).show();
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
    }
    else
    {
        Toast.makeText(this, "Has Permissions" , Toast.LENGTH_LONG).show();
    }
}
like image 147
Vishal Chhodwani Avatar answered Nov 20 '25 17:11

Vishal Chhodwani


If permission > 1, than it has to combined together at Runtime.

According to Google, beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time, even if the app targets a lower API level. You should test your app to verify that it behaves properly when it’s missing a needed permission, regardless of what API level your app targets.

Things to keep in mind while asking Multiple Permission:

  1. Number of Permission.
  2. Never Ask Again Checkox

Look at the Source for Multiple Permission Request

like image 25
W4R10CK Avatar answered Nov 20 '25 17:11

W4R10CK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!