Sendra Falvia Sendra Falvia - 19 days ago 5
Android Question

Android multiple permission error

The situation : When camera(imageview) is click, it will prompt multiple permission.


  1. If user select DENY on 1st permission and 2nd permission, it's working.

  2. If user select ALLOW on 1st permission and 2nd permission, its working.

  3. If user select DENY on 1st permission and ALLOW on 2nd permission, it's working.

  4. If user select ALLOW on 1st permission and DENY on 2nd permission, error will appear.



Here is code (extends Fragment) :

private static String[] PERMISSIONS_CAMERA = {Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};


img_1 = (ImageView) view.findViewById(R.id.img_1);
img_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startCamera();
}
});



private void StartCamera() {
requestPermissions(PERMISSIONS_CAMERA, REQUEST_CAMERA);
}

private void OpenCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
this.bitmap = null;
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode) {
// For camera
case REQUEST_CAMERA:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
OpenCamera();
} else {
Toast.makeText(getActivity(), "Go to settings and enable permissions", Toast.LENGTH_LONG).show();
}
break;
}

}


Error Log :


java.lang.RuntimeException: Failure delivering result
ResultInfo{who=@android:requestPermissions:, request=131073,
result=-1, data=Intent {
act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to
activity {app.swb.mont/app.swb.mont.ui.activity.MainActivity}:
java.lang.SecurityException: Permission Denial: starting Intent {
act=android.media.action.IMAGE_CAPTURE
cmp=com.sec.android.app.camera/.Camera } from ProcessRecord{b2f7ceb
28260:app.swb.mont/u0a392} (pid=28260, uid=10392) with revoked
permission android.permission.CAMERA
at android.app.ActivityThread.deliverResults(ActivityThread.java:4925)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4968)
at android.app.ActivityThread.access$1600(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1849)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE
cmp=com.sec.android.app.camera/.Camera } from ProcessRecord{b2f7ceb
28260:app.swb.mont/u0a392} (pid=28260, uid=10392) with revoked
permission android.permission.CAMERA

Answer

The problem is that your are not handling the onRequestPermissionResult correctly. On this line

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                OpenCamera();}

Your are checking only the first permission that your requested witch is in this case Manifest.permission.READ_EXTERNAL_STORAGE (position == 0) and you trigger your camera function witch require the permission at position == 2

Solution

if (grantResults.length > 0 && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
                OpenCamera();}

OR

if (grantResults.length > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED) ) {
                OpenCamera();}

// You can also use a loop