美文网首页
Android 7.0 FileUriExposedExcept

Android 7.0 FileUriExposedExcept

作者: 李虎子 | 来源:发表于2018-03-21 11:52 被阅读0次

    Android 7.0为了提高私有文件的安全性进行了系统权限更改。当尝试把file://URI发到应用外会触发FileUriExposedException,官方推荐方法是使用FileProvider。(详情:https://developer.android.com/reference/android/support/v4/content/FileProvider.html

    使用方法:
    1,在AndroidManifest.xml配置FileProvider
    <provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.xxx.xxx.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/file_path" />
    </provider>

    这里需要把com.xxx.xxx改成自己应用的包名。
    2,在资源文件的xml目录下创建file_path.xml文件
    <?xml version="1.0" encoding="utf-8"?>
    <paths>
    <root-path
    name="file_path"
    path="."/>
    </paths>
    3,生成Uri
    Uri uri = FileProvider.getUriForFile(context, "com.xxx.xxx.fileprovider", file);
    第二个参数authority要和第一步的配置里android:authorities的值保持一致。

    demo:https://github.com/lihuzi19/FileDemoApplication

    相关文章

      网友评论

          本文标题:Android 7.0 FileUriExposedExcept

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