Android FileProvider笔记

一、FileProvider是什么

通过FileProvider.getUriForFile(@NonNull Context context, @NonNull String authority, @NonNull File file)方法获得一个有临时权限的Uri给客户端用来访问本APP文件。

当然看FileProvider类的注释更加详细

二、代码示例

XML 复制代码
<provider
     android:name="androidx.core.content.FileProvider"
     android:authorities="com.mydomain.fileprovider"
     android:exported="false"
     android:grantUriPermissions="true">
     <meta-data
         android:name="android.support.FILE_PROVIDER_PATHS"
         android:resource="@xml/file_paths" />
 </provider>
XML 复制代码
//file_paths内容
<paths>
	<files-path
            name="moon"
            path="myDir/" />
</paths>
java 复制代码
File dir = new File(context.getFileDir(),"myDir");
FIle file = new File(dir,"my.png");
Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", file);
//得到的就是: content://com.mydomain.fileprovider/moon/my.png

三、冲突解决

  1. 当多个SDK都包含androidx.core.content.FileProvider,并且authorities相同时,可以采用把多个SDK的file_paths文件内容合并的方式处理。
  2. 当多个SDK都包含androidx.core.content.FileProvider,并且authorities不同时,可以采取自定义FileProvider的方式,每个file_paths对应一个FileProvider。
相关推荐
Name_NaN_None2 小时前
安卓手机远程控制电脑方法(Android/Windows) —— 「小白教程」
android·windows·智能手机·电脑·笔记本
恋猫de小郭2 小时前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
Z21-X3 小时前
云原生运维学习笔记:Kubernetes Service 发现、类型与 kube-proxy 实战
运维·笔记·学习·云原生·kubernetes
sealaugh323 小时前
powerbi学习笔记(day2)powerbi进阶
笔记·学习·powerbi
一只大鸽子3 小时前
游戏开发笔记11-Godot RPG游戏基础-优化-四方向攻击
笔记·游戏·godot
敢敢のwings12 小时前
GR00T 2:当机器人学会先想象未来,再决定怎么动
android·机器人
传奇开心果编程12 小时前
【Jetpack Compose娓娓道来】第55课:传感器与设备姿态——把物理世界的数据接进Compose
android·kotlin·移动开发·传感器·jetpack compose·ui框架·声明式ui
传奇开心果编程14 小时前
【Jetpack Compose娓娓道来】第52课:蓝牙与位置权限——把“附近”和“在哪”安全地接进Compose
android·kotlin·移动开发·蓝牙·jetpack compose·声明式ui·位置权限
alexhilton14 小时前
卡顿定位:从 UI 线程到 RenderThread
android·kotlin·android jetpack
FungLeo15 小时前
FengCMS· PHP+MySQL升级实录 · 后台参数统一治理:id[]数组、id=undefined 跳转与312组回调组合
android·mysql·php·工程实践·fengcms