本文主要是介绍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><paths></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踩过的坑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!