我正在使用 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>
文件路径.xml:
<paths>
<files-path name="img" path="images/" />
</paths>
爪哇:
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);
相机活动开始,拍照成功,但活动结果不正常,我在错误日志中看到的唯一内容是
CAM_StateSavePic:将结果保存到 URI 时出现异常:Optional.of(content://com.mypackage.fileprovider/img/cameraOutput1469383289530.jpg) FileNotFoundException:是目录
编辑 错过 File#createNewFile();