bug exposed beyond app through Intent.getData()

转载大神,用于自己学习

今天在做项目功能的时候遇到一个bug exposed beyond app through Intent.getData()

在项目中点击文件路径跳转过去,编译器报错android os FileUriExposedException

导致错误的原因是没有使用FileProvider

在应用间共享文件

对于面向 Android 7.0 的应用,Android 框架执行的 StrictMode API 政策禁止在您的应用外部公开 file:// URI。如果一项包含文件 URI 的 intent 离开您的应用,则应用出现故障,并出现 FileUriExposedException 异常。

解决方法

在AndroidManifest中配置 FileProvider

复制代码
<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths_public" />
        </provider>

其中${applicationId}一般指向包名

不是androidx的可以参考

复制代码
<provider
    android:name="android.support.v4.content.FileProvider"
     android:authorities="${applicationId}.provider"
      android:exported="false"
    android:grantUriPermissions="true">
      <meta-data
         android:name="android.support.FILE_PROVIDER_PATHS"
         android:resource="@xml/file_paths_public" />
</provider>

还需要res下创建xml文件夹,并在xml文件夹下创建file_paths_public.xml

复制代码
 <!--1、对应内部内存卡根目录:Context.getFileDir()-->
    <files-path
        name="int_root"
        path="/" />
    <!--2、对应应用默认缓存根目录:Context.getCacheDir()-->
    <cache-path
        name="app_cache"
        path="/" />
    <!--3、对应外部内存卡根目录:Environment.getExternalStorageDirectory()-->
    <external-path
        name="ext_root"
        path="pictures/" />
    <!--4、对应外部内存卡根目录下的APP公共目录:Context.getExternalFileDir(String)-->
    <external-files-path
        name="ext_pub"
        path="/" />
    <!--5、对应外部内存卡根目录下的APP缓存目录:Context.getExternalCacheDir()-->
    <external-cache-path
        name="ext_cache"
        path="/" />

使用方法

复制代码
 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  Uri contentUri = FileProvider
  .getUriForFile(OrderManagerActivity.this, "com.jk.house", file);
  intent.setDataAndType(contentUri, "*/*");

作者:吕氏春秋

链接:https://juejin.cn/post/6939823618918449166

相关推荐
androidwork6 小时前
Android LinearLayout、FrameLayout、RelativeLayout、ConstraintLayout大混战
android·java·kotlin·androidx
每次的天空6 小时前
Android第十三次面试总结基础
android·面试·职场和发展
wu_android6 小时前
Android 相对布局管理器(RelativeLayout)
android
李斯维8 小时前
循序渐进 Android Binder(二):传递自定义对象和 AIDL 回调
android·java·android studio
androidwork8 小时前
OkHttp 3.0源码解析:从设计理念到核心实现
android·java·okhttp·kotlin
像风一样自由9 小时前
【001】frida API分类 总览
android·frida
casual_clover9 小时前
Android 之 kotlin 语言学习笔记四(Android KTX)
android·学习·kotlin
移动开发者1号11 小时前
Android 大文件分块上传实战:突破表单数据限制的完整方案
android·java·kotlin
移动开发者1号11 小时前
单线程模型中消息机制解析
android·kotlin
每次的天空13 小时前
Android第十五次面试总结(第三方组件和adb命令)
android