美文网首页
FileProvider:Android7之后应用间共享文件

FileProvider:Android7之后应用间共享文件

作者: littlefogcat | 来源:发表于2023-02-01 10:13 被阅读0次

    https://developer.android.google.cn/training/secure-file-sharing?hl=zh-cn

    在 Android 7 之前,文件之间可以通过 file:// 协议共享本地文件:

    但在Android7及之后的版本中,文件之间共享文件受到了严格的限制,无法直接使用file://协议来共享文件了。所以需要使用 FileProvider 来共享文件。

    使用 FileProvider 需要进行以下几步:

    1. 定义要共享的文件夹

    新建xml文件 file_paths.xml,内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-files-path
            name="resource_root"
            path="resource/" />
    </paths>
    

    其中,name 为自定义的共享名称,path 为共享的文件夹名称(如果共享根目录,则设置为 path=".")。external-files-path 标签表示外部存储中的应用私有目录。以上代码表示的即是,共享外部私有目录中的 resource 文件夹。

    标签名称包含以下几类:

    The <paths> element must contain one or more of the following child elements:

    <files-path name="name" path="path" />
    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().

    <cache-path name="name" path="path" />
    Represents files in the cache subdirectory of your app's internal storage area. The root path of this subdirectory is the same as the value returned by getCacheDir().

    <external-path name="name" path="path" />
    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" />
    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).

    <external-cache-path name="name" path="path" />
    Represents files in the root of your app's external cache area. The root path of this subdirectory is the same as the value returned by Context.getExternalCacheDir().

    <external-media-path name="name" path="path" />
    Represents files in the root of your app's external media area. The root path of this subdirectory is the same as the value returned by the first result of Context.getExternalMediaDirs().

    例如,我想共享的是我应用的 Context.getFilesDir() 中的 image 目录,则修改 xml 文件为:

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <files-path
            name="whatever_name_you_want"
            path="image/" />
    </paths>
    

    2. 新建自定义FileProvider类

    新建类MyFileProvider,继承自FileProvider。这个类里面无需实现任何内容。

    public class MyFileProvider extends FileProvider {
    }
    

    3. 在Manifest中添加自定义的FileProvider

    AndroidManifest.xml 的 application 标签内,添加如下内容:

            <provider
                android:name=".MyFileProvider"
                android:authorities="com.example.fileprovider.MyFileProvider"
                android:exported="false"
                android:grantUriPermissions="true">
    
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/file_paths" />
            </provider>
    

    其中,android:authorities 中填写的是一个用于寻找 Provider 的自定义字符串,类似于 intent 的 action。这里一般填写类的全名即可。
    android:grantUriPermissions="true" 这句是必须填写的,否则不被允许使用该 Provider 共享 uri。
    meta-data 中的内容,android.support.FILE_PROVIDER_PATHS 是固定的字符串,照着写就行;而 "@xml/file_paths" 则是步骤 1 中创建的 xml 文件。

    4. 启动目标Activity

    根据之前的步骤,我们就可以向目标 Activity 共享需要的文件了。

    // 想要共享的文件
    File file = getSharedFile();
    // 通过authority对应的FileProvider,将File转换成Uri。注意这里的authority需要和步骤3中的相同
    Uri uri = FileProvider.getUriForFile(mContext, "com.example.fileprovider.MyFileProvider");
    // 目标intent
    Intent intent = new Intent(ACTION_MY_TARGET);
    // 必须添加以下flag,以允许目标读写文件
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    // 设置文件的uri
    intent.setData(mUri);
    // 启动对应Activity
    mContext.startActivity(intent);
    

    要点有几项:

    1. 通过 FileProvider 获取文件的 uri;
    2. 在 Intent 中添加允许读写的 flag;
    3. 通过 Intent.setData 设置文件的 uri;

    另外需要注意的是,目标 Activity 需要在 intent-filter 中定义 data 标签,并至少定义 mimeType,表示可处理的文件类型:

    <data android:mimeType="*/*" />
    

    否则,可能会出现异常。

    相关文章

      网友评论

          本文标题:FileProvider:Android7之后应用间共享文件

          本文链接:https://www.haomeiwen.com/subject/rmkihdtx.html