Android 7.0 FileProvider踩过的坑

2023-12-02 02:58
文章标签 android 7.0 fileprovider

本文主要是介绍Android 7.0 FileProvider踩过的坑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言:下面记录两个在7.0系统之后使用FileProvider遇到的问题

问题一:

Error:C:***AndroidManifest.xml:352:13-62 Error:Attribute provider#android.support.v4.content.FileProvider@authorities value=(***.fileProvider) from AndroidManifest.xml:352:13-62is also present at [com.jph.takephoto:takephoto_library:4.0.3] AndroidManifest.xml:19:13-64 value=(***.fileprovider).Suggestion: add 'tools:replace="android:authorities"' to <provider> element at AndroidManifest.xml:350:9-358:20 to override.
FAILURE: Build failed with an exception.

清单文件配置如下:

   <providerandroid:name="android.support.v4.content.FileProvider"android:authorities="***.fileProvider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_paths" /></provider>

这个问题上面的报错信息里其实已经说的很明确了,反复添加

android:authorities="***.fileProvider"

出现的原因是因为我的项目添加了一个第三方的库,而第三方库的清单文件里也添加了这段代码,导致了冲突。

解决方法在错误日志中也给出了:在我们自己的清单文件中添加下面代码:

xmlns:tools="http://schemas.android.com/tools"
tools:replace="android:authorities"

添加后的代码如下:

   <providerandroid:name="android.support.v4.content.FileProvider"android:authorities="***.fileProvider"android:exported="false"android:grantUriPermissions="true"xmlns:tools="http://schemas.android.com/tools"tools:replace="android:authorities"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_paths" /></provider>

重新编译通过。

问题二:

          清单文件里对FileProvider的注册和问题一修复后的代码一致。

         代码中的使用如下:

         Uri uri = FileProvider.getUriForFile( this, "***.filerovider", mTempFile);

每次运行到这段代码的时候都会报错,导致app崩溃:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object referenceat android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)

进入FileProvider.getUriForFile();查看一个源码:

/*** Return a content URI for a given {@link File}. Specific temporary* permissions for the content URI can be set with* {@link Context#grantUriPermission(String, Uri, int)}, or added* to an {@link Intent} by calling {@link Intent#setData(Uri) setData()} and then* {@link Intent#setFlags(int) setFlags()}; in both cases, the applicable flags are* {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} and* {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION}. A FileProvider can only return a* <code>content</code> {@link Uri} for file paths defined in their <code>&lt;paths&gt;</code>* meta-data element. See the Class Overview for more information.** @param context A {@link Context} for the current component.* @param authority The authority of a {@link FileProvider} defined in a*            {@code <provider>} element in your app's manifest.* @param file A {@link File} pointing to the filename for which you want a* <code>content</code> {@link Uri}.* @return A content URI for the file.* @throws IllegalArgumentException When the given {@link File} is outside* the paths supported by the provider.*/
public static Uri getUriForFile(Context context, String authority, File file) {final PathStrategy strategy = getPathStrategy(context, authority);return strategy.getUriForFile(file);
}

主要看一下对第二个参数的介绍,大致意思是说:这里的参数需要和清单文件里的

android:authorities="***.fileProvider"

 保持一致。好吧,问题就是出在这里,authorities要一模一样,包括大小写。

这篇关于Android 7.0 FileProvider踩过的坑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/443691

相关文章

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

如何在Ubuntu 24.04上部署Zabbix 7.0对服务器进行监控

《如何在Ubuntu24.04上部署Zabbix7.0对服务器进行监控》在Ubuntu24.04上部署Zabbix7.0监控阿里云ECS服务器,需配置MariaDB数据库、开放10050/1005... 目录软硬件信息部署步骤步骤 1:安装并配置mariadb步骤 2:安装Zabbix 7.0 Server

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I