我已经按照这个 Google 教程开始了使用new Intent(MediaStore.ACTION_IMAGE_CAPTURE)
. 本教程建议使用getExternalStoragePublicDirectory
对我的应用非常有用的公共目录。但随后他们的示例改为使用getExternalFilesDir
. 然后要将文件的 URI 传递给意图,MediaStore.EXTRA_OUTPUT
我必须获取一个内容 URI,因为我想以 Android N 为目标。在定位 NI 之前,只需传递一个 file:// URI,除了 Google 之外的所有人都很高兴。现在对 NI 开始弄了FileUriExposedException
,好像不是很优惠。
所以鉴于我有这样的文件......
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyAppFolder");
if (!storageDir.exists() && !storageDir.mkdir())
Log.w(TAG, "Couldn't create photo folder: " + storageDir.getAbsolutePath());
File image = new File(storageDir, timeStamp + ".jpg");
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
...我可以使用公共图片目录的内置提供程序来获取内容 URI 吗?如果有怎么办?
我试过类似的东西
takePictureIntent.putExtra(
MediaStore.EXTRA_OUTPUT,
FileProvider.getUriForFile(this, MediaStore.AUTHORITY, createImageFile()));
但它只是抛出
IllegalArgumentException:缺少 android.support.FILE_PROVIDER_PATHS 元数据
那个权威是正确的吗?如果我必须使用自己的提供程序来共享公共文件,那么我可以在 FILE_PROVIDER_PATHS 元数据中指定什么路径?我能找到的所有选项都是针对私有目录的。