如何修复相机权限指甲

时间:2019-05-20 06:16:29

标签: android android-sqlite

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.systems.subrapos, PID: 3637
    java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=android/com.android.internal.app.ResolverActivity } from ProcessRecord{4010923 3637:com.systems.subrapos/u0a320} (pid=3637, uid=10320) with revoked permission android.permission.CAMERA
        at android.os.Parcel.readException(Parcel.java:1715)
        at android.os.Parcel.readException(Parcel.java:1668)
        at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3175)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1520)
        at android.app.Activity.startActivityForResult(Activity.java:4399)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
        at android.app.Activity.startActivityForResult(Activity.java:4357)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
        at com.systems.subrapos.activity.ProductAddStep3Activity.openCamera(ProductAddStep3Activity.java:246)
        at com.systems.subrapos.activity.ProductAddStep3Activity.access$000(ProductAddStep3Activity.java:29)
        at com.systems.subrapos.activity.ProductAddStep3Activity$1.onClick(ProductAddStep3Activity.java:78)
        at android.view.View.performClick(View.java:5647)
        at android.view.View$PerformClick.run(View.java:22465)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:163)
        at android.app.ActivityThread.main(ActivityThread.java:6393)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Application terminated.

3 个答案:

答案 0 :(得分:0)

请求权限:

ActivityCompat.requestPermissions(activity, arrayOf("permission"), requestCode)

在通话活动中:

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

      if(ContextCompat.checkSelfPermission(context, "permission") == PERMISSION_GRANTED) {
      // start intent
      }
...
}

如果用户已授予权限,则无需请求。

请确保您输入了丢失的权限:)如果这有帮助,请不要忘记将我的答案设置为批准:)或者如果没有帮助,请与我联系!

答案 1 :(得分:0)

详细

  • 如果Android手机运行的是棉花糖或更高版本。对于诸如ACTION_IMAGE_CAPTURE之类的权限,应该在运行时询问。
  • 您可以使用来检查Android版本

    如果(Build.VERSION.SDK_INT> = Build.VERSION_CODES.M){ //然后请求各自的许可 }

  • 这是来自官方网站https://developer.android.com/training/permissions/requesting

  • 的运行时权限的链接

答案 2 :(得分:0)

Try this,
private void openCamera() { 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermission();
    }
} 

public void requestPermission() {       
    if (ContextCompat.checkSelfPermission((Activity) context,
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) this.context,
                Manifest.permission.CAMERA)) {
            ActivityCompat.requestPermissions((Activity) this.context,
                    new String[] { Manifest.permission.CAMERA }, Constants.PERMISSION_REQUEST_CAMERA);              
        } else {
        //This will request for permisson
            ActivityCompat.requestPermissions((Activity) this.context,
                    new String[] { Manifest.permission.CAMERA }, Constants.PERMISSION_REQUEST_CAMERA);              
        }
    }       
}   

//Callback for requestPermissions
@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        switch (requestCode) { 
            case Constants.PERMISSION_REQUEST_CAMERA: {             
                if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.i("open camera permission has been granted");      
                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);           
                } else {
                    Log.i("open camera permission has been denied");
                    //Dont call camera intent app will crash                    
                }
                return;
            }          
        }
    }