Android用FileProvider拍照

Iho*_*mov 5 android android-camera android-fileprovider

我正在使用FileProvider在Android Nougat上拍照,这是我的代码

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mypackage.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)

file_paths.xml:

<paths>
    <files-path name="img" path="images/" />
</paths>
Run Code Online (Sandbox Code Playgroud)

Java的:

 String fileName = "cameraOutput" + System.currentTimeMillis() + ".jpg";
 File imagePath = new File(getContext().getFilesDir(), "images");
 File file = new File(imagePath, fileName);
 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

 final Uri outputUri = FileProvider.getUriForFile(activity, "com.mypackage.fileprovider", file);
 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
 getContext().grantUriPermission(
            "com.google.android.GoogleCamera",
            outputUri,
            Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION
 );
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            cameraIntent.setFlags(FLAG_GRANT_WRITE_URI_PERMISSION);
 activity.startActivityForResult(cameraIntent, ACTIVITY_REQUEST_CODE);
Run Code Online (Sandbox Code Playgroud)

相机活动开始,成功拍照,但活动结果不正常,我在错误日志中看到的唯一的事情是

CAM_StateSavePic:将结果保存到URI时发生异常:Optional.of(content://com.mypackage.fileprovider/img/cameraOutput1469383289530.jpg)FileNotFoundException:是一个目录

编辑 错过了File#createNewFile();

Rah*_*yay 10

使相机和图库URI工作的主要方面是提供者路径.

你需要创建一个provider_paths.xml到/ res/xml/dir

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
Run Code Online (Sandbox Code Playgroud)

在清单文件中添加此行

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
   <meta-data
       android:name="android.support.FILE_PROVIDER_PATHS"
       android:resource="@xml/provider_paths">
</provider>
Run Code Online (Sandbox Code Playgroud)

还有一件事,我发现我们需要像这样在Application类中设置vmPolicy

@Override
public void onCreate() {
    super.onCreate();
   //Allowing Strict mode policy for Nougat support
   StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
   StrictMode.setVmPolicy(builder.build());
}
Run Code Online (Sandbox Code Playgroud)

仔细检查清单文件中的相机和外部存储权限,然后使用nougat URI.

有关更多详细信息,请查看此链接:Android N FileUriExposedException