29

I get ERROR revoked permission android.permission.CAMERA

I was using camera permission on manifest

private void dispatchTakePictureIntent(int actionCode) {

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    switch (actionCode) {
        case ACTION_TAKE_PHOTO_B:
            File f = null;

            try {
                f = setUpPhotoFile();
                mCurrentPhotoPath = f.getAbsolutePath();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            } catch (IOException e) {
                e.printStackTrace();
                f = null;
                mCurrentPhotoPath = null;
            }
            break;

        default:
            break;
    } // switch

    startActivityForResult(takePictureIntent, actionCode);
}

and error like this:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.mobile.lunatique.photo, PID: 3590
                  java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.android.camera2/com.android.camera.CaptureActivity clip={text/uri-list U:file:///KTP_42342.jpg} (has extras) } from ProcessRecord{2fbbd91 3590:com.mobile.lunatique.photo/u0a60} (pid=3590, uid=10060) with revoked permission android.permission.CAMERA
                      at android.os.Parcel.readException(Parcel.java:1599)
                      at android.os.Parcel.readException(Parcel.java:1552)
                      at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2658)
                      at android.app.Instrumentation.execStartActivity(Instrumentation.java:1507)
                      at android.app.Activity.startActivityForResult(Activity.java:3917)
                      at android.app.Activity.startActivityForResult(Activity.java:3877)
                      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
                      at com.mobile.lunatique.photo.MainActivity.dispatchTakePictureIntent(MainActivity.java:254)
                      at com.mobile.lunatique.photo.MainActivity.access$000(MainActivity.java:33)
                      at com.mobile.lunatique.photo.MainActivity$1.onClick(MainActivity.java:321)
                      at android.view.View.performClick(View.java:5198)
                      at android.view.View$PerformClick.run(View.java:21147)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/Process: Sending signal. PID: 3590 SIG: 9

anyone can help me??

Qasim
  • 5,181
  • 4
  • 30
  • 51
  • Post stacktrace. – azizbekian Mar 27 '17 at 10:03
  • Too much irrelevant code, too little relevant stuff like stacktrace and explanations. – Vucko Mar 27 '17 at 10:07
  • 1
    In Marshamallow, user should activate the permission at run time. See here : https://developer.android.com/training/permissions/requesting.html. Or you can set your targetSdkVersion in the gradle to 22 (It is not recommended but it works) – Imene Noomene Mar 27 '17 at 11:10

5 Answers5

77

Here's how I solved this problem.

Contrary to what most answers say, REMOVE this from your Manifest file.

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

Why?

Starting Android M (API 23), if your app has CAMERA permission declared in the manifest, then, it needs that CAMERA permission to be GRANTED in order to access ACTION_IMAGE_CAPTURE etc... too (which normally do not require the CAMERA permission on their own). If not, then it automatically raises a SecurityException.

╔═════════════════════════════════════════════════╦════════════════════╗
║                      Usage                      ║ Permissions needed ║
╠═════════════════════════════════════════════════╬════════════════════╣
║ ACTION_IMAGE_CAPTURE                            ║ none               ║
║ ACTION_VIDEO_CAPTURE                            ║ none               ║
║ INTENT_ACTION_STILL_IMAGE_CAMERA                ║ none               ║
║ android.hardware.camera2                        ║ CAMERA             ║
║ android.hardware.camera2 + ACTION_IMAGE_CAPTURE ║ CAMERA             ║
║ android.hardware.camera2 + ACTION_VIDEO_CAPTURE ║ CAMERA             ║
║ ...                                             ║ ...                ║
╚═════════════════════════════════════════════════╩════════════════════╝

Above table is only for API 23+

tl;dr What to do?

Option 1- If you only use ACTION_IMAGE_CAPTURE etc...
Remove the CAMERA permission from manifest and you would be fine

Option 2- If you use other CAMERA functions:
Check for CAMERA permission at runtime and only start the intent when the permission is available

Kathir
  • 1,282
  • 1
  • 15
  • 19
  • Man I searched for this for hours! Thanks a lot! – Zuks Apr 28 '20 at 16:12
  • Thnaks a Lot. I was stuck 4 to 5 hours. – Pawan Soni Nov 10 '20 at 17:25
  • Can u please elaborate why need to remove camera permission? @Kathir – Pawan Soni Nov 10 '20 at 17:26
  • @PawanSoni I have reformatted my answer. Please let me know if it's better now. – Kathir Nov 11 '20 at 06:45
  • I have no sense of this behavior ... – saiday Sep 28 '21 at 10:07
  • We are using a library that uses `ACTION_IMAGE_CAPTURE` which can crop images from file store in the device storage or images captured using camera. However it is crashing (same as above issue) when `CAMERA` permission is added in app module. But we need `CAMERA` for other feature of the app not just for the image cropping library. Are you saying we should ask for `CAMERA` permission also when using the library to avoid the issue? – Bitwise DEVS Jul 24 '22 at 19:07
  • 1
    @BitwiseDEVS Yes, that should probably fix the crash (if that was the actual reason behind the crash) – Kathir Jul 31 '22 at 14:40
  • @Kathir thanks, by the way may I know how are you able to produce that command line table? – Bitwise DEVS Jul 31 '22 at 17:25
  • @BitwiseDEVS I generated that table based off the behaviour back then. It's not from the documentation – Kathir Nov 17 '22 at 06:02
18

Hi lunatique aldebaran,

First of all you need to add the following permission in manifest.xml file.

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

Check whether the android device is Marshmallow or not. If its android M or greater version then use the following code to get camera permission dynamically.

Before android M dont want to get dynamic permission.

check if the user grant the permission. If user not granted permission then request the camera permission access.

private static final int MY_CAMERA_REQUEST_CODE = 100;
if (checkSelfPermission(Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {
           requestPermissions(new String[]{Manifest.permission.CAMERA},
                        MY_CAMERA_REQUEST_CODE);
            }

After requesting the permission alert will display to ask permission contains allow and deny options. After clicking action we can get result of the request with the following method.

@Override

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

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == MY_CAMERA_REQUEST_CODE) {

            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();

            } else {

                Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();

            }

        }
Sathish Kumar VG
  • 2,154
  • 1
  • 12
  • 19
  • 1
    THIS would be the accepted answer. Very complete. Thank you. – bbruno5 Sep 04 '19 at 02:21
  • 1
    The point is that taking a picture via intent should not require this permission at all, but like the accepted answer mentions, somehow the permission must be granted if you have it in the manifest. – bairprogramming Jun 10 '20 at 08:23
1

You should use AppCompatActivity instead, of extending your MainActivity. And also please, include stacktrace.

public class MainActivity extends AppCompatActivity {

//......
}

Oh, sorry. With stacktrace it's clearly seen main problem. android.permission.CAMERA. For making request, you should create permission check. Please, check This Example, to create Permission checking.

Example:

1) Include to Activity.

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

2) Ensure you request permissions from the user:

if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) {

            ActivityCompat.requestPermissions( this, new String[] {  android.Manifest.permission.ACCESS_COARSE_LOCATION  },
                                                LocationService.MY_PERMISSION_ACCESS_COURSE_LOCATION );
}

3) Check in RunTime

if ( Build.VERSION.SDK_INT >= 23 &&
             ContextCompat.checkSelfPermission( context, android.Manifest.permission.CAMERA ) != PackageManager.PERMISSION_GRANTED) {
            return;
}
GensaGames
  • 5,538
  • 4
  • 24
  • 53
1

Revoke Permission....It Simply say's you have invoked the permission again so no need to write permission for Camera again in Manifest file ......

Instead you just....need to write two line....to READ and Write External Storage which are enough to run your program it's enough to run your program

Viraj
  • 21
  • 1
1

I had similar issue but it looks like Android has a bug with this. When you add camera permission to AndroidManifest

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

You have to grant permission for it even you want to use external application for taking a photo. Deleting it from manifest has solved problem with SecurityException.