当前位置: 首页 > news >正文

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

相关文章:

  • 从哪里下载WinPrefetchView最安全?
  • [Ubuntu] 记录系统崩掉进入initramfs模式后的解决方法
  • Linux:进程信号(二.信号的保存与处理、递达、volatile关键字、SIGCHLD信号)
  • Mind 爱好者周刊 第13期 | 人类神经重放和尖波涟漪综述、饥饿对记忆巩固的影响、使用AI从大脑中解码语言、B族维生素与神经精神疾病的关系……
  • 【生成模型】【ComfyUI(三)】使用WebAPI批量调用ComfyUI
  • Linux发展史、发行版本、特性以及应用场景
  • Revisiting Reverse Distillation for Anomaly Detection
  • 在CentOS7上部署与关闭Flask接口
  • 视频孪生技术赋能文旅数字化转型:重构景区体验与管理新模式
  • 嵌入式开发:傅里叶变换(5):STM32和Matlab联调验证FFT
  • MobSF(Mobile Security Framework) 的详细介绍、安装指南、配置说明
  • 物联网平台建设方案一
  • 力扣提升第一天
  • 在linux中利用conda安装blast
  • openssh9.9p2部分Linux操作系统的rpm包
  • Spring boot中的@ConfigurationProperties注解
  • 完全背包问题
  • SQL注入(order by,limit),seacms的报错注入以及系统库的绕过
  • deepseek部署:ELK + Filebeat + Zookeeper + Kafka
  • [C++][cmake]使用C++部署yolov12目标检测的tensorrt模型支持图片视频推理windows测试通过
  • wordpress 获取评论/优化标题关键词技巧
  • 做视频网站成本/天津网站排名提升多少钱
  • 山东华邦建设网站首页/小红书关键词排名
  • 郑州市做网站的公/网站seo内容优化
  • oss可以做网站根目录吗/推广普通话手抄报句子
  • ps切片工具做网站/网络公司名字