如何使用XML路径文件来保存图片

时间:2017-11-22 08:44:41

标签: android xml path

我创建了一个这样的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="my_images" path="Android/data/myApp/files/Pictures" />
</paths>

现在,我想要的是使用此文件夹存储我的应用程序拍摄的照片,但我不知道如何将其作为默认文件夹来执行此操作。我尝试将文件夹写为文件路径,但抛出IllegalArgumentException,因为该文件夹似乎还不存在。

1 个答案:

答案 0 :(得分:0)

首先,它用于分享目的,以隐藏其他人的实际路径。

FileProvider是ContentProvider的一个特殊子类,它通过为文件而不是文件创建内容来促进与应用程序关联的文件的安全共享:// Uri。

您需要将此文件保留在xml目录下。 水库&GT; XML的yourXmlName 您需要在androidManifest.xml中添加元数据

<application>
    ...
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mydomain.fileprovider"//(your projcet package name)
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/yourXmlName" />
    </provider>
    ...
</application>

现在你可以使用这样的路径..

File imagePath = new File(Context.getFilesDir(), "Android/data/myApp/files/Pictures");
File newFile = new File(imagePath, "default_image.jpg");
Uri contentUri = getUriForFile(getContext(),   "your_project_package_name", newFile);

输出将是:

content://your_project_package_name/my_images/default_image.jpg

它将隐藏你的洞路径...... For Details about it.