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使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

2025最新版Android Studio安装及组件配置教程(SDK、JDK、Gradle)

《2025最新版AndroidStudio安装及组件配置教程(SDK、JDK、Gradle)》:本文主要介绍2025最新版AndroidStudio安装及组件配置(SDK、JDK、Gradle... 目录原生 android 简介Android Studio必备组件一、Android Studio安装二、A

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

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