Android:如何获取内容://外部存储PUBLIC目录中文件的URI

Pau*_*aul 18 android android-intent android-contentprovider android-7.0-nougat

我已经按照这个Google教程开始了一个捕获图像的意图new Intent(MediaStore.ACTION_IMAGE_CAPTURE).本教程建议使用getExternalStoragePublicDirectory适用于我的应用程序的公共目录.但后来他们的例子反而使用了getExternalFilesDir.然后将文件的URI传递给intent,MediaStore.EXTRA_OUTPUT必须得到一个内容URI,因为我想要定位Android N.在定位NI之前,只需传递一个文件:// 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;
}
Run Code Online (Sandbox Code Playgroud)

...我可以使用公共图片目录的内置提供程序来获取内容URI吗?如果是这样的话?

我尝试过类似的东西

takePictureIntent.putExtra(
    MediaStore.EXTRA_OUTPUT, 
    FileProvider.getUriForFile(this, MediaStore.AUTHORITY, createImageFile()));
Run Code Online (Sandbox Code Playgroud)

但它只是抛出

IllegalArgumentException:缺少android.support.FILE_PROVIDER_PATHS元数据

该权威是否正确?如果我必须使用自己的提供程序来共享公共文件,那么我可以在FILE_PROVIDER_PATHS元数据中指定哪条路径?我能找到的所有选项都是针对私人目录的.

pum*_*e65 12

TL:DR <external-path name="MyAppFolder" path="." />有效,但是安全吗?

你需要自己做FileProvider.值得一读它的文档,但这里有一个简短的概述.

正如@CommonsWare所提到的,你需要用MediaStore.AUTHORITY你的例子替换"com.example.fileprovider",你将在你的清单中定义权限,就像这样......

<application>AndroidManifest 的标记内,键入以下内容:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.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)

您获得的异常是由于没有<meta-data>链接到xml文件的标记,该文件包含FileProvider允许您触摸的所有路径.但这就是我自己一直在努力争取的文件.

在链接的教程结束时,在" 保存全尺寸照片"部分的末尾,Google为此提供的示例file_paths.xml是:

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

"my_images""MyAppFolder"在您的示例中替换.在Google的那篇教程中声称

路径组件对应于使用Environment.DIRECTORY_PICTURES调用时由getExternalFilesDir()返回的路径.确保将com.example.package.name替换为应用的实际包名称.

...并打字输出让我意识到这并不是指你和我正在寻找的公共图片目录.这解释了为什么使用该路径会产生此异常:

java.lang.IllegalArgumentException:无法找到包含/storage/emulated/0/Pictures/MyAppFolder/{filename}.jpg的已配置根目录

我将把它留在这里以供将来参考,然后继续回答您的大问题 - 您可以指定哪条路径允许其他应用与您的应用目标API级别24+创建的公共图片目录中的文件进行交互?

正如@CommonsWare所说,"所需子目录的详细信息因设备而异",因此您无法声明公共图片目录的路径,但我找到了一种有效的方法.

<external-path name="MyAppFolder" path="." />将允许您FileProvider为其他应用程序(如相机)提供内容URI,然后可以读取和写入它解析的文件.

如果这有任何危险或缺点,我很乐意听到他们.


Sta*_*ots 5

这是我在我的“图片”公共目录应用程序中使用的内容。

  1. 像这样创建文件:

    File theFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "YOUR_APP_NAME" + File.separator + "YOUR_FILE_NAME");
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在文件提供程序的元数据中指定以下路径:

    <paths>
        <external-path name="secure_name" path="Pictures/YOUR_APP_NAME" />
    </paths>
    
    Run Code Online (Sandbox Code Playgroud)

出于安全原因,“ secure_name”用于隐藏要共享的子目录的名称(在本例中为“ YOUR_APP_NAME”)。

“图片”对应于Environment.DIRECTORY_PICTURES

  1. 获得Uri发送Intent

    final Uri theUri = FileProvider.getUriForFile(activity,
                        getString(R.string.fileprovider_authorities),
                        theFile);
    theIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    theIntent.putExtra(MediaStore.EXTRA_OUTPUT, theUri);
    
    Run Code Online (Sandbox Code Playgroud)