打开相机并捕获图像问题

时间:2018-05-04 09:20:07

标签: android

我正在尝试制作一个使用系统相机应用程序拍照的简单应用程序。我在Android版本6中测试了我的应用程序,我的应用程序成功打开了设备的相机。但在一些Android v6设备(如三星Galaxy A3)中,我得到以下安全例外:

java.lang.SecurityException: 
     at android.os.Parcel.readException (Parcel.java:1621)
     at android.os.Parcel.readException (Parcel.java:1574)
     at android.app.ActivityManagerProxy.startActivity (ActivityManagerNative.java:3182)
     at android.app.Instrumentation.execStartActivity (Instrumentation.java:1541)
     at android.app.Activity.startActivityForResult (Activity.java:4298)
     at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult (BaseFragmentActivityJB.java:50)
     at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:79)
     at android.app.Activity.startActivityForResult (Activity.java:4245)
     at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:859)
     at iforest.photogps.MainActivity.captureImage (MainActivity.java:203)
     at iforest.photogps.MainActivity.access$000 (MainActivity.java:73)
     at iforest.photogps.MainActivity$1.onClick (MainActivity.java:130)
     at android.view.View.performClick (View.java:5721)
     at android.widget.TextView.performClick (TextView.java:10930)
     at android.view.View$PerformClick.run (View.java:22620)
     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:7331)
     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)

我已经在我的代码中添加了权限检查:

if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED 
         && ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
         && ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    //TO do here if permission is granted by user
} else {
    //ask for permission if user didnot given
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    }
}

以后我称之为捕获图像:

private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    File photoFile = null;
    try {
        photoFile = createImageFile();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    //fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    String authorities = getApplicationContext().getPackageName() + ".provider";
    Uri imageUri = FileProvider.getUriForFile(this, authorities, photoFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);  <-- line 203

}

从日志中我看到带有startActivityForResult的captureImage第203行被调用。由于我已经要求相机的权限,为什么我在特定设备Samsung Galaxy A3 -Android 6中获得SecurityException以打开相机?在具有相同版本的其他设备中,打开相机不会发生错误。

1 个答案:

答案 0 :(得分:0)

  

我正在尝试制作一个使用系统相机应用程序拍摄照片的简单应用程序

没有单一的系统相机应用程序&#34;。您的代码将启动用户想要使用的任何相机应用程序,并且将有数百个这样的应用程序分布在大约10,000个Android设备型号和约20亿个Android设备上。

  

由于我已经要求相机的权限,为什么我会在特定设备Samsung Galaxy A3 -Android 6中使用SecurityException打开相机?

替换:

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

使用:

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

看看是否有帮助。

相关问题