Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to implement camera permissions for android M getting No virtual method checkSelfPermission

I tried implementing camera permissions in this way:

private void checkCameraPermissions() {
        if(checkCameraHardware(this)) {
            if (checkSelfPermission(Manifest.permission_group.camera)
                    != PackageManager.PERMISSION_GRANTED) {

                Crashlytics.log("Requesting camera permission");

                // Should we show an explanation?
                if (shouldShowRequestPermissionRationale(
                        Manifest.permission_group.camera)) {
                    // Explain to the user why we need to use the camera
                    showRationaleForCameraUse();
                }

                requestPermissions(new String[]{Manifest.permission_group.camera},
                        Constants.MY_PERMISSIONS_REQUEST_CAMERA_GROUP);

                // MY_PERMISSIONS_REQUEST_CAMERA_GROUP is an
                // app-defined int constant
            } else {
                startApp();
            }
        }else{
            showNoCameraAvailableAlert();
        }
    }

@Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case Constants.MY_PERMISSIONS_REQUEST_CAMERA_GROUP: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay!
                    // start the app
                    startApp();

                } else {

                    // permission denied, boo!
                    // leav the app!
                    showNoCameraAvailableAlert();
                }
                return;
            }

            // other 'switch' lines to check for other
            // permissions this app might request
        }
    }

and got this following error:

java.lang.NoSuchMethodError: No virtual method checkSelfPermission(Ljava/lang/String;)I in class Lim/emu/app/emu/app/view/splash/SplashActivity; or its super classes (declaration of 'im.emu.app.emu.app.view.splash.SplashActivity' appears in /data/app/im.emu.app.emu.test-1/base.apk)
            at im.emu.app.emu.app.view.splash.SplashActivity.checkCameraPermissions(SplashActivity.java:74)
            at im.emu.app.emu.app.view.splash.SplashActivity.onCreate(SplashActivity.java:52)
            at android.app.Activity.performCreate(Activity.java:5977)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2367)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5274)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
08-30 16:34:10.801  17087-17357/im.emu.app.emu.test I/Fabric﹕ Crashlytics report upload complete: 55E30629033A-0001-42BF-296ED8AC703A.cls

Any ideas how I can fix this? p.s. if any more information is needed please ask

like image 823
dangalg Avatar asked Aug 30 '15 13:08

dangalg


People also ask

How to check permission granted or not in Android?

To check if the user has already granted your app a particular permission, pass that permission into the ContextCompat. checkSelfPermission() method. This method returns either PERMISSION_GRANTED or PERMISSION_DENIED , depending on whether your app has the permission.

What is runtime permission in Android?

Runtime permissions prevent apps from gaining access to private data without a user's consent, and provide them with additional context and visibility into the types of permissions that applications are either seeking, or have been granted.

How to ask for multiple permissions in Android?

In case one or more permissions are not granted, ActivityCompat. requestPermissions() will request permissions and the control goes to onRequestPermissionsResult() callback method. You should check the value of shouldShowRequestPermissionRationale() flag in onRequestPermissionsResult() callback method.


2 Answers

You should be running the code on device running previous versions. You have to use ContextCompat.checkSelfPermission() instead.

like image 146
Derek Fung Avatar answered Oct 17 '22 11:10

Derek Fung


Careful with

'shouldShowRequestPermissionRationale(
                        Manifest.permission_group.camera)'   

I had an issue with the permissions group where it wanted the individual permission and not the group and would throw an exception if you used the other.

like image 22
Travis Castillo Avatar answered Oct 17 '22 10:10

Travis Castillo