再会。
使用从开发人员指南获得的帮助,我能够让我的应用程序的照片捕捉工作。该活动通常涉及捕获一张或多张图像(例如在 photogrid 相机拼贴应用程序中)。它大部分时间都可以在大多数设备上运行,除了我自己的设备(Android 5.0.1)。问题是,相机/图库应用程序崩溃,在堆栈跟踪中给我一个安全权限拒绝错误,我已经搜索了解决方案并实施了它们,但无济于事。崩溃通常是随机的,但更有可能发生在以下情况: 1. 我正在拍摄第二张照片 2. 我的相机设置为最高分辨率:13MP(在较低分辨率下几乎不会发生)
虽然在其他情况下很少发生,但它的出现让我发疯。这是我的代码:
分段
private void dispatchTakePictureIntent(boolean isQuestionImage) {
Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile(isQuestionImage);
} catch (IOException ex) {
Log.e("photofile part", "Error: "+ex);
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(getActivity(),
"com.operator.u_learn.fileprovider", photoFile);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
List<ResolveInfo> resolvedIntentActivities = getActivity()
.getPackageManager()
.queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
String packageName = resolvedIntentInfo.activityInfo.packageName;
getActivity().grantUriPermission(packageName,
photoURI,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
if(isQuestionImage)
startActivityForResult(takePictureIntent, REQUEST_QUESTION_IMAGE_CAPTURE);
else
startActivityForResult(takePictureIntent, REQUEST_EXPLANATION_IMAGE_CAPTURE);
//revoke permissions after use
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
getActivity().revokeUriPermission(photoURI, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
}
}
AndroidManifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.operator.u_learn.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-
data>
</provider>
文件路径.xml
<?xml version="1.0" encoding="utf-8"?>
<paths
xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
path="Android/data/com.operator.u_learn/files/Pictures" />
</paths>
这是我得到的错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process:
com.android.gallery3d, PID: 18974
java.lang.SecurityException: Permission Denial: opening
provider android.support.v4.content.FileProvider from
ProcessRecord{3b596555 18974:com.android.gallery3d/u0a42}
(pid=18974, uid=10042) that is not exported from uid 10221
at
android.os.Parcel.readException(Parcel.java:1540)
at
android.os.Parcel.readException(Parcel.java:1493)