尝试保存文件时相机崩溃

Camera crashes, when trying to save fileI

我卡住了。这是我的方法,从相机获取照片并保存它们:

> > @Override
>     public File createImageFile() throws IOException {
>         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
>         String imageFileName = "JPEG_" + timeStamp + "_";
>         File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
>         File imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);
>         model.currentPhotoPath = imageFile.getAbsolutePath();
>         return imageFile;
>     }

@Override
public Intent getCameraPictureIntent(PackageManager packageManager) {
    Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (pictureIntent.resolveActivity(packageManager) != null) {
        try {
            model.userPicFile = createImageFile();
        } catch (IOException e) {
            Log.i(TAG, "Can't create file!");
        }
        if (model.userPicFile != null) {
            model.userPicFileUri = FileProvider.getUriForFile(context,
                    "com.internship.droidz.talkin.fileprovider",
                    model.userPicFile);
            pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, model.userPicFileUri);
        }
    }
    return pictureIntent;
}

我正在创建临时文件,然后获取 uri,将其设置为 intent。然后 startActivity 获取结果。当我拍照并提交时,应用程序崩溃了。 Logcat什么也没说。

这里是存储的文件和uri:

    public class RegistrationModel implements RegistrationContract.RegistrationModel {

    String currentPhotoPath;
    Uri userPicFileUri;
    File userPicFile;
}

这是 Manifest.xml 中的权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.camera"
    android:required="false" />

编译 API 19+

即 Log.i 表示:

01-30 14:39:18.721 3265-3265/com.android.camera E/AndroidRuntime: FATAL EXCEPTION: main
                                                              Process: com.android.camera, PID: 3265
                                                              java.lang.IllegalStateException: Could not execute method of the activity
                                                                  at android.view.View.onClick(View.java:3823)
                                                                  at android.view.View.performClick(View.java:4438)
                                                                  at android.view.View$PerformClick.run(View.java:18422)
                                                                  at android.os.Handler.handleCallback(Handler.java:733)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:136)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                  at dalvik.system.NativeStart.main(Native Method)
                                                               Caused by: java.lang.reflect.InvocationTargetException
                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
                                                                  at android.view.View.onClick(View.java:3818)
                                                                  at android.view.View.performClick(View.java:4438) 
                                                                  at android.view.View$PerformClick.run(View.java:18422) 
                                                                  at android.os.Handler.handleCallback(Handler.java:733) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                  at android.os.Looper.loop(Looper.java:136) 
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                  at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                  at dalvik.system.NativeStart.main(Native Method) 
                                                               Caused by: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{52a0a5b0 3265:com.android.camera/u0a36} (pid=3265, uid=10036) that is not exported from uid 10058
                                                                  at android.os.Parcel.readException(Parcel.java:1465)
                                                                  at android.os.Parcel.readException(Parcel.java:1419)
                                                                  at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2848)
                                                                  at android.app.ActivityThread.acquireProvider(ActivityThread.java:4399)
                                                                  at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2208)
                                                                  at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425)
                                                                  at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:906)
                                                                  at android.content.ContentResolver.openOutputStream(ContentResolver.java:669)
                                                                  at android.content.ContentResolver.openOutputStream(ContentResolver.java:645)
                                                                  at com.android.camera.Camera.doAttach(Camera.java:1385)
                                                                  at com.android.camera.Camera.onReviewDoneClicked(Camera.java:1362)
                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                  at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                  at android.view.View.onClick(View.java:3818) 
                                                                  at android.view.View.performClick(View.java:4438) 
                                                                  at android.view.View$PerformClick.run(View.java:18422) 
                                                                  at android.os.Handler.handleCallback(Handler.java:733) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                  at android.os.Looper.loop(Looper.java:136) 
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                  at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                  at dalvik.system.NativeStart.main(Native Method) 

感谢大家!我使用答案 Android - file provider - permission denial

解决了这个问题
    List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
    String packageName = resolveInfo.activityInfo.packageName;
    context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}