我正在尝试制作一个使用系统相机应用程序拍照的简单应用程序。我已经在 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。既然我已经询问了相机的权限,为什么我得到 SecurityException 来在特定设备三星 Galaxy A3 -Android 6 中打开相机?在其他相同版本的设备上打开相机没有错误。