Android FileProvider.getUriForFile抛出null对象异常

问题描述 投票:0回答:1

我正在扩展我的Android Cordova插件以使用相机。在广泛阅读有关该主题的各种Android开发人员文章以及此论坛上的主题后,我已完成以下操作

在我的插件config.xml文件中

<provider android:name="android.support.v4.content.FileProvider" 
 android:authorities="com.example.app.camera" android:exported="false" 
 android:grantUriPermissions="true">
 <meta-data android:name="android.support.FILE_PROVIDER_PATHS" 
  android:resource="@xml/reviewpics"/>
 </provider>

随着

<resource-file src="src/android/xml/reviewpics.xml" 
  target="res/xml/reviewpics.xml" />

为了更好的衡量,我将已编译的APK打开为zip存档,并检查res / xml / reviewpics.xml确实存在。

reviewpics.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
 <files-path name="camera" path="."/>
</paths>

camera的身份分享应用程序的根内部存储位置。

在应用程序本身我做了以下

File photo = new File(...);
 Uri uri = 
FileProvider.getUriForFile(cordova.getActivity(),
"com.example.app.camera",photo);

但是,当我编译,安装并运行它时,我始终得到消息

尝试在空对象引用上调用虚方法'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,java.lang.String)'

很明显,我做错了,虽然我觉得我已经按照这个问题的各种例子和教程。谁能告诉我这里缺少什么?

cordova android-camera android-fileprovider
1个回答
0
投票

我在这里留下这个问题并为遇到类似问题的其他人提供答案。这里要记住的关键点是上下文 - 一个Cordova插件。 Cordova documentation on plugin.xml最好是粗略的,并留下了很多细节。但是,应该记住,有必要告诉Cordova使用哪些Android项目配置文件来放置您可能通过plugin.xml提供的任何指令。我省略了这样做的结果

<provider>...

块根本就没有进入任何配置文件。一旦我把<provider>...包裹在一个

<config-file target="AndroidManifest.xml" parent="application">
...
</config-file>

阻止一切正常工作!

© www.soinside.com 2019 - 2024. All rights reserved.