集成环信聊天功能时,打开相机崩溃的解决

在集成环信的聊天功能时,打开相机程序就崩溃,原因是Android 7.0后直接使用本地真实路径的Uri被认为是不安全的,因此使用一种特殊的内容提供器FileProvider,它可以选择性地将封装过的Uri共享给外部,从而提高了应用的安全性。因此,没有提供FileProvider的程序运行在Android 7.0以上的系统会报错,报错信息如下:

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)

点进报错信息的位置,定位到EaseCompat类中

return FileProvider.getUriForFile(context, "****.fileprovider", file);

这句代码
那么,为什么会报空指针异常呢?我们再查看到getUriForFile的源码:

     * @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);
    }

注意到第二行指出了这里的authority要和app的manifest.xml文件内provider中写的authority完全相同,大小写也必须一样。
因此,如果没有在manifest.xml内提供provider的代码,需要在其application块内添加的provider代码如下(要注意的是,这一段代码是写在程序主Module的app目录下的manifest.xml中):

<provider
    android:name="android.support.v4.content.FileProvider"
    tools:replace="android:authorities"
    android:authorities="***.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文件是用来指定Uri共享的,name值可以随便填,path值表示共享的具体路径,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="user_images" path="/" />
</paths>

因此,抛出这个异常的原因是provider块中authority的名字不同引起的,只需要把抛出异常位置

return FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", file);

的authority更换为一致即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值