Android:自定义外部存储文件夹上的FileProvider [英] Android : FileProvider on custom external storage folder

查看:772
本文介绍了Android:自定义外部存储文件夹上的FileProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个文件共享器来共享文件.我的文件保存在外部存储的"AppName"文件夹中(与Android,Movies和Pictures文件夹级别相同).

I'm trying to set up a fileprovider for sharing file. My files are saved in a folder "AppName" in the external storage (same level as Android, Movies and Pictures folders).

这是我的文件提供程序配置:

Here is my file provider config :

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.mydomain.appname.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"/>
</provider>

和file_paths.xml:

and the file_paths.xml :

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="mypath" path="AppName" />
</paths>

当我尝试使用:p来访问我的文件时

When i try to access my file with :

Uri fileUri = FileProvider.getUriForFile(activity, "com.mydomain.appname.fileprovider",
            new File("/storage/emulated/0/AppName/IMG_20160419_095211.jpg"));

它返回一个错误: java.lang.IllegalArgumentException: 无法找到包含/storage/emulated/0/AppName/IMG_20160419_095211.jpg的已配置根目录 在android.support.v4.content.FileProvider $ SimplePathStrategy.getUriForFile(FileProvider.java:678) 在android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)

It returns an error: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/AppName/IMG_20160419_095211.jpg at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678) at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)

在使用诸如Pictures或Movies之类的内置目录之前,它工作得很好,我的file_paths.xml定义如下:

It worked fine before when I was using built-in directory like Pictures or Movies, my file_paths.xml was define like this :

<external-path name="photos" path="Pictures" />
<external-path name="videos" path="Movies" />

但是现在我想将文件存储在我自己的文件夹中.我是否错过了FileProvider配置?

But now I want to store my file in my own folder. Did I miss something with the FileProvider config ?

推荐答案

<files-path name="name" path="path" />  

代表应用程序内部存储区的files/子目录中的文件.该子目录与值相同 由Context.getFilesDir()返回.

Represents files in the files/ subdirectory of your app's internal storage area. This subdirectory is the same as the value returned by Context.getFilesDir().

<external-path name="name" path="path" />

表示外部存储区根目录中的文件.根 该子目录的路径与的返回值相同 Environment.getExternalStorageDirectory().

Represents files in the root of the external storage area. The root path of this subdirectory is the same as the value returned by Environment.getExternalStorageDirectory().

<external-files-path name="name" path="path" />

代表应用程序外部存储区根目录中的文件.这 该子目录的根路径与的返回值相同 Context#getExternalFilesDir(String)Context.getExternalFilesDir(null).

Represents files in the root of your app's external storage area. The root path of this subdirectory is the same as the value returned by Context#getExternalFilesDir(String) Context.getExternalFilesDir(null).

有关更多详细信息,请检查 Android的FileProvider文档. 像下面的图片这样的一些配置,com.view.asim.enterprise是我的软件包名称.

for more details please check Android's doc of FileProvider. some configuration like following picture,com.view.asim.enterprise is my package name.

这篇关于Android:自定义外部存储文件夹上的FileProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆