如何解决SecurityException问题?

时间:2016-11-16 13:51:40

标签: android

我使用Fabric作为错误跟踪器,我有这样的错误

Fatal Exception: java.lang.SecurityException: Lacking privileges to access camera service
   at android.hardware.camera2.utils.CameraBinderDecorator.throwOnError(CameraBinderDecorator.java:108)
   at android.hardware.camera2.legacy.CameraDeviceUserShim.connectBinderShim(CameraDeviceUserShim.java:331)
   at android.hardware.camera2.CameraManager.openCameraDeviceUserAsync(CameraManager.java:283)
   at android.hardware.camera2.CameraManager.openCamera(CameraManager.java:399)
   at com.fittingroom.newtimezone.view.ActivityCamera.openCamera(ActivityCamera.java:498)
   at com.fittingroom.newtimezone.view.ActivityCamera.access$000(ActivityCamera.java:73)
   at com.fittingroom.newtimezone.view.ActivityCamera$1.onSurfaceTextureAvailable(ActivityCamera.java:120)
   at android.view.TextureView.getHardwareLayer(TextureView.java:375)
   at android.view.View.updateDisplayListIfDirty(View.java:14224)
   at android.view.View.getDisplayList(View.java:14284)
   at android.view.View.draw(View.java:15069)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3533)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3327)
   at android.view.View.updateDisplayListIfDirty(View.java:14246)
   at android.view.View.getDisplayList(View.java:14284)
   at android.view.View.draw(View.java:15069)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3533)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3327)
   at android.view.View.updateDisplayListIfDirty(View.java:14246)
   at android.view.View.getDisplayList(View.java:14284)
   at android.view.View.draw(View.java:15069)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3533)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3327)
   at android.view.View.updateDisplayListIfDirty(View.java:14246)
   at android.view.View.getDisplayList(View.java:14284)
   at android.view.View.draw(View.java:15069)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3533)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3327)
   at android.view.View.updateDisplayListIfDirty(View.java:14246)
   at android.view.View.getDisplayList(View.java:14284)
   at android.view.View.draw(View.java:15069)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3533)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3327)
   at android.view.View.updateDisplayListIfDirty(View.java:14246)
   at android.view.View.getDisplayList(View.java:14284)
   at android.view.View.draw(View.java:15069)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3533)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3327)
   at android.view.View.updateDisplayListIfDirty(View.java:14246)
   at android.view.View.getDisplayList(View.java:14284)
   at android.view.View.draw(View.java:15069)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3533)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3327)
   at android.view.View.updateDisplayListIfDirty(View.java:14246)
   at android.view.View.getDisplayList(View.java:14284)
   at android.view.View.draw(View.java:15069)
   at android.view.ViewGroup.drawChild(ViewGroup.java:3533)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3327)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchDraw(PhoneWindow.java:2700)
   at android.view.View.draw(View.java:15376)
   at android.widget.FrameLayout.draw(FrameLayout.java:652)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2679)
   at android.view.View.updateDisplayListIfDirty(View.java:14254)
   at android.view.View.getDisplayList(View.java:14284)
   at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:272)
   at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:278)
   at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:317)
   at android.view.ViewRootImpl.draw(ViewRootImpl.java:2899)
   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2736)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2351)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1284)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6490)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:802)
   at android.view.Choreographer.doCallbacks(Choreographer.java:605)
   at android.view.Choreographer.doFrame(Choreographer.java:574)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:788)
   at android.os.Handler.handleCallback(Handler.java:815)
   at android.os.Handler.dispatchMessage(Handler.java:104)
   at android.os.Looper.loop(Looper.java:194)
   at android.app.ActivityThread.main(ActivityThread.java:5546)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:967)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

据我所知,这个错误与java已经保留的类的命名相关...

但是如何知道我要改名的名字?

提前致谢!

1 个答案:

答案 0 :(得分:1)

这里我创建了一个类来检查和请求权限

public class MarshMallowPermission {

    public static final int RECORD_PERMISSION_REQUEST_CODE = 1;
    public static final int WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2;
    public static final int READ_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 3;
    public static final int CAMERA_PERMISSION_REQUEST_CODE = 4;
    Activity activity;

    public MarshMallowPermission(Activity activity) {
        this.activity = activity;
    }

    public boolean checkPermissionForRecord(){
        int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO);
        if (result == PackageManager.PERMISSION_GRANTED){
            return true;
        } else {
            return false;
        }
    }

    public boolean checkPermissionForWriteExternalStorage(){
        int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (result == PackageManager.PERMISSION_GRANTED){
            return true;
        } else {
            return false;
        }
    }
    public boolean checkPermissionForReadExternalStorage(){
        int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
        if (result == PackageManager.PERMISSION_GRANTED){
            return true;
        } else {
            return false;
        }
    }

    public boolean checkPermissionForCamera(){
        int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
        if (result == PackageManager.PERMISSION_GRANTED){
            return true;
        } else {
            return false;
        }
    }

    public void requestPermissionForRecord(){
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO)){
            Toast.makeText(activity, "Microphone permission needed for recording. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.RECORD_AUDIO},RECORD_PERMISSION_REQUEST_CODE);
        }
    }

    public void requestPermissionForWriteExternalStorage(){
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
            Toast.makeText(activity, "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
        }
    }
    public void requestPermissionForReadExternalStorage(){
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_EXTERNAL_STORAGE)){
            Toast.makeText(activity, "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},READ_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
        }
    }

    public void requestPermissionForCamera(){
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)){
            Toast.makeText(activity, "Camera permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CAMERA},CAMERA_PERMISSION_REQUEST_CODE);
        }
    }
}

以及如何在活动中使用它

 private void selectImage() {
        final CharSequence[] items = {str_camera, str_image_lib,
                str_cancel};

        AlertDialog.Builder builder = new AlertDialog.Builder(UploadActivityClaim.this);
        builder.setTitle("Add Files!");
        builder.setItems(items, (dialog, item) -> {
            if (items[item].equals(str_camera)) {
                if (!marshMallowPermission.checkPermissionForCamera()) {
                    marshMallowPermission.requestPermissionForCamera();
                    return;
                }
                dispatchTakePictureIntent();
            } else if (items[item].equals(str_image_lib)) {
                if (!marshMallowPermission.checkPermissionForReadExternalStorage()) {
                    marshMallowPermission.requestPermissionForReadExternalStorage();
                    return;
                }
                enableBrowseFile();
            } else if (items[item].equals(str_cancel)) {
                dialog.dismiss();
            }
        });
        builder.show();
    }