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-62
	is 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.

清单文件配置如下:

   <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="***.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android: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"

添加后的代码如下:

   <provider
            android: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-data
                android: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 reference
                                                                  at 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要一模一样,包括大小写。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值