之前集成环信时没有问题,将androidstudio项目更新到androidx的时候环信的拍照功能异常崩溃了,第一想法就是7.0以上权限申请问题,原因是Android 7.0后直接使用本地真实路径的Uri被认为是不安全的,因此使用一种特殊的内容提供器FileProvider,它可以选择性地将封装过的Uri共享给外部,从而提高了应用的安全性。因此,没有提供FileProvider的程序运行在Android 7.0以上的系统会报错
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="androidx.core.content.FileProvider"-->
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更换为一致即可,fileprovider这里最好也小写。
还有个小问题,改完后还是报错,报个huawei什么的message
android:name="com.huawei.hms.update.provider.UpdateProvider"
android:authorities="com.mochain.mofnance.hms.update.provider"
android:exported="false"
android:grantUriPermissions="true" />
把这段注释掉了,跑项目没问题了,后来把这个又放出来了,项目也可以跑。
网友评论