bug exposed beyond app through Intent.getData()

devtools/2025/2/26 6:29:09/

转载大神,用于自己学习
今天在做项目功能的时候遇到一个bug exposed beyond app through Intent.getData()
在项目中点击文件路径跳转过去,编译器报错android os FileUriExposedException
导致错误的原因是没有使用FileProvider
在应用间共享文件
对于面向 Android 7.0 的应用,Android 框架执行的 StrictMode API 政策禁止在您的应用外部公开 file:// URI。如果一项包含文件 URI 的 intent 离开您的应用,则应用出现故障,并出现 FileUriExposedException 异常。
解决方法
在AndroidManifest中配置 FileProvider

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

其中${applicationId}一般指向包名
不是androidx的可以参考

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

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

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


http://www.ppmy.cn/devtools/162744.html

相关文章

Python将Box企业网盘里一个目录下所有文件上载到S3并导入Redshift

import configparser import os import logging import threading import time import boto3 from ftplib import FTP_TLS from botocore.exceptions import NoCredentialsError from concurrent.futures import ThreadPoolExecutor# 配置日志 logging.basicConfig(filenameupl…

筑牢 YMatrix 质量防线:从测试出发(思路篇)

前言 随着数据库产品的复杂性和迭代速度的增加&#xff0c;质量问题逐渐成为开发过程中的一大挑战。测试作为确保产品质量的关键环节&#xff0c;不仅能够有效预防潜在缺陷&#xff0c;还能提升开发效率和客户满意度。 本文将从测试的重要性出发&#xff0c;探讨如何通过系统…

AI顿悟之旅 - 1 - DeepSeek的训练方法为什么相比GPT-o1大幅度减少算力资源?

DeepSeek R1 模型和 GPT-3 模型在训练方法上有一些关键的不同&#xff0c;这些不同也使得 DeepSeek R1 能够大幅降低训练成本。 用简单易懂的语言为你解释一下&#xff1a; GPT-3 的训练方法: 预测下一个词 (Next Word Prediction): GPT-3 和它的前辈 GPT-2 一样&#xff0c…

UVM_CALLBACK 应用举例

UVM_CALLBACK是一种基于回调函数的设计模式&#xff0c;允许用户在特定事件发生时插入自定义的行为。UVM提供了uvm_callback类作为基类&#xff0c;用户可以通过继承该类来定义自己的回调行为。采用uvm_callback基类&#xff0c;用户可以在不更改原始代码的情况下轻松插入调试代…

AI自动化爬虫项目对比报告

摘要 本报告旨在深入研究AI自动化爬虫项目,对比分析其在实现方式、效率提升、自托管能力等方面的差异。 随着大数据和人工智能技术的快速发展,传统网络爬虫技术面临着越来越多的挑战,如网站反爬虫机制的加强、网页结构复杂多变等。AI自动化爬虫技术应运而生,利用机器学习、…

Python 开发 creo 详细版

好的,以下是脚本的完整代码内容: from win32com import client import VBAPI from tkinter import messagebox, filedialog, Tk, Button, Entry, Label import os CREO_APP = C:/PTC/Creo 2.0/Parametric/bin/parametric.exe PART_DIR = D:/mydoc/creo_python/fin.prt OUTP…

OpenCV计算摄影学(2)图像去噪函数denoise_TVL1()

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 原始-对偶算法是用于解决特定类型变分问题&#xff08;即&#xff0c;寻找一个函数以最小化某个泛函&#xff09;的算法。特别地&#xff0c;图像…

webdriver-manager

webdriver-manager是一个用于管理Selenium WebDriver的命令行工具&#xff0c;它可以帮助用户安装、更新和启动Selenium WebDriver。以下是对webdriver-manager的详细解释&#xff1a; 一、webdriver-manager的用途 自动下载WebDriver&#xff1a;webdriver-manager可以自动检…