FelRPI
FelRPI

Reputation: 429

Android requestPermission Dialog not showing up

I am working with a Samsung Galaxy Tab S tablet and try to grant permission to write on the external storage at runtime.

However the dialog to request the permission does not show up. Instead in the code onRequestPermissionResult will be executed instantly with the a PERMISSION_DENIED result. I even tried granting the storage permission manually in the application settings on the device, but even then the result will be PERMISSION_DENIED (grantResults is -1).

My code looks like this:

public class MainActivity extends AppCompatActivity {

    public static final int REQUEST_STORAGE_PERMISSION = 225;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {
                process();
            } else {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_STORAGE_PERMISSION:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_LONG).show();
                    process();

                } else {
                    Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_LONG).show();
                }
        }
    }


    public void process() {
        //...
    }

}

When starting the application, it will instantly show the "Permission Denied"-Toast, without showing me any dialog before. That happens, even if i grant the Storage-permission manually in the app settings of the tablet.

My manifest-file also contains the permission at the right place (outside of the application tags) as suggested in similar threads.

I tried reinstalling the app and even reset the tablet already, but nothing seems to work.

Upvotes: 0

Views: 1154

Answers (2)

raj kavadia
raj kavadia

Reputation: 1073

Do this thing

private boolean RequestPermissions() {

int camera = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA);
int storage = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
List<String> listPermissionsNeeded = new ArrayList<>();
if (camera != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(CAMERA);
}
if (storage != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(WRITE_EXTERNAL_STORAGE);
    listPermissionsNeeded.add(READ_EXTERNAL_STORAGE);

}
if (!listPermissionsNeeded.isEmpty()) {
    ActivityCompat.requestPermissions(getActivity(), listPermissionsNeeded.toArray
            (new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
    return false;
}

return true;

}

Upvotes: 0

rafsanahmad007
rafsanahmad007

Reputation: 23881

instead of:

 requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION); 

it works for fragments

You need to use this: As you are using AppCompatActivity

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION);

EDIT

As you have multiple project module: you need to add permission in both of those Manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

Upvotes: 1

Related Questions