【发布时间】:2016-10-23 12:52:15
【问题描述】:
我已关注this Google tutorial,开始使用new Intent(MediaStore.ACTION_IMAGE_CAPTURE) 捕获图像。本教程建议使用带有getExternalStoragePublicDirectory 的公共目录,这对我的应用程序非常有用。但随后他们的示例改为使用getExternalFilesDir。然后用MediaStore.EXTRA_OUTPUT 将文件的URI 传递给intent,我have to get a content URI 因为我想以Android N 为目标。在以N 为目标之前,我只需传递一个file:// URI,除了Google 之外的所有人都很高兴。现在在 N 上,我开始收到 FileUriExposedException,它似乎是 not very favorable。
所以鉴于我有这样的文件...
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 元数据中指定什么路径?所有的options I can find 都是私有目录。
【问题讨论】:
-
“这个权限正确吗?” ——不,因为你不是
MediaStore。 “如果我必须使用自己的提供程序来共享公共文件,那么我可以在我的 FILE_PROVIDER_PATHS 元数据中指定什么路径?” --<external-path>为您提供外部存储的根。FileProvider无法指定您要专门使用的目录。 -
@CommonsWare 如果 FileProvider 无法使用该目录,那么我唯一的选择是使用私有目录还是停止以 N 为目标?这似乎很愚蠢。感谢您的建议。
-
“如果 FileProvider 无法使用该目录”——这不是我写的。话虽如此,由于您所需子目录的详细信息因设备而异,因此尝试使用
<external-path>会有风险。 “那么我唯一的选择是使用私人目录还是停止定位 N?” - 不。你可以写你自己的ContentProvider。或者,您可以为我的StreamProvider编写一个插件。或者您可以在外部存储上使用自定义目录,您可以从外部存储根目录控制其路径(尽管这可能是您所说的“私有目录”)。 -
感谢 CommonsWare 并对我的误解深表歉意。在我找到更好的方法之前,我将在可以使用
FileProvider托管的目录中创建文件。然后在onActivityResult中,我可以将文件复制到外部存储公共目录中的最终存放位置。自定义和第三方 ContentProvider 似乎对这项工作有点繁重。
标签: android android-intent android-contentprovider android-7.0-nougat