Android FileProvider崩溃-npe试图在空字符串上调用XmlResourceParser
这是我清单的一部分:Android FileProvider崩溃-npe试图在空字符串上调用XmlResourceParser,android,Android,这是我清单的一部分: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.asd" android:versionCode="118" android:versionName="118" > <uses-sdk a
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asd"
android:versionCode="118"
android:versionName="118" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:name="com.example.asd.AsdApplication"
android:allowBackup="true"
android:allowTaskReparenting="true"
android:theme="@style/AsdTheme" >
...
<provider
android:name="com.example.asd.database.hq.ContentProviderDB"
android:authorities="ourContentProviderAuthorities" >
</provider>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.asd.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
...
</application>
</manifest>
我试着这样使用它:
private void playVideoFromDeviceWithWorkaround(String fileName) {
File newFile = new File(getFilesDir(), fileName);
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
try {
vvVideoFullscreen.setVideoURI(contentUri);
showMediaControls = true;
playVideo();
} catch (Exception e) {
playVideoFromNetwork();
}
}
在这一行:
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
android:authorities="com.example.asd.fileprovider"
我得到以下错误:
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:560)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)
问题是在清单中我有这样一行:
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
android:authorities="com.example.asd.fileprovider"
调用getUriForFile时,我经过:
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
因此,将
“com.example.asd”
更改为“com.example.asd.fileprovider”
,它起到了作用您无需硬编码程序包名称,还可以在同一台设备上运行多个变体(请考虑发行版
和调试
和使用应用程序UFFIX
,请参阅):
基于FileProvider.java:560
final ProviderInfo info = context.getPackageManager()
.resolveContentProvider(authority, PackageManager.GET_META_DATA);
final XmlResourceParser in = info.loadXmlMetaData( //560
context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
您使用了错误的权限
,它没有找到内容提供程序
(info==null
)
将清单更改为(${applicationId}
将被清单合并替换)
及
.share
后缀是可选的,以防您有一个真正的ContentProvider
,最好将包名作为权限。首先,确保您的提供商android:authorities
与您的其他提供商不冲突。除此之外,您可以为其名称的最后一部分选择任何名称:“提供者”、“文件提供者”等,但当列出多个android:authorities
时,应用程序会崩溃,而文档说明它允许列出多个值
文件://
方案现在不允许在targetSdkVersion>=24(Android N 7.0)上附加,只有内容://
始终传递给所有设备(Android 5、6和7)。但我们遇到小米打破了谷歌的惯例,发送了文件://
,因此数据。getData().getAuthority()
给出了空字符串
final String uriScheme = currentUri.getScheme();
if ("content".equals(uriScheme)) {
// getting full file path (works with some providers, i.e. Gallery)
path = FileUtils.getPath(getContext(), currentUri);
if (path != null) {
currentFile = new File(path);
}
} else if ("file".equals(uriScheme)) {
// in a rare case we received file:// in currentUri, we need to:
// 1. create new File variable from currentUri that looks like "file:///storage/emulated/0/download/50044382b.jpg"
// 2. generate a proper content:// Uri for it
currentFile = new File(currentUri.getPath());
String authority = data.getData().getAuthority();
if (authority != null && !authority.isEmpty()) {
currentUri = FileProvider.getUriForFile(getActivity(), authority, currentFile);
}
} else {
// throw exception
}
此外,当FileProvider.getUriForFile()
导致崩溃java.lang.IllegalArgumentException:无法找到包含/storage/emulated/0/Android/data/com的配置根目录。示例/files/attachments/image.jpg
已在Android支持库v24.2.0中修复。问题是FileProvider.java没有看到外部路径文件夹。如果您在运行时使用BuildConfig
构建权限,请确保使用包括包名在内的完整类名
坏的:
final String AUTHORITY=BuildConfig.APPLICATION_ID+“.provider”代码>
好:
final String AUTHORITY=com.mycompany.myapp.BuildConfig.APPLICATION_ID+“.provider”代码>下面的代码对我很有用
mUri = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".provider",
fileObject);
在我的例子中,我得到错误是因为
BuildConfig.APPLICATION_ID
是从中国进口的
import android.support.v4.BuildConfig;
因此,它返回的字符串是“android.support.v4”
,而不是我的项目包名称。检查导入文件是否来自您的import project.Buildconfig
,而不是其他文件。例如:
import com.example.yourProjectName.BuildConfig;
最后,在清单中的
标记中,我有android:authorities=“${applicationId}”
始终将我的项目包名称作为权限
<manifest>
..
..
<application>
..
..
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/ruta_fileprovider" />
</provider>
</application>
</manifest>
..
..
..
..
以下是我为解决此问题所做的工作。我在android中给出了完全限定名:name。它在安卓6,7,8中工作
<provider android:authorities="${applicationId}.opener.provider"
android:exported="false" android:grantUriPermissions="true"
android:name="io.github.pwlin.cordova.plugins.fileopener2.FileProvider">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
</provider>
您应该试试:
Context context = PostAdapter.this.activity;
StringBuilder stringBuilder2 = new StringBuilder();
stringBuilder2.append(PostAdapter.this.activity.getPackageName());
stringBuilder2.append(".provider");
Uri uri;
uri = FileProvider.getUriForFile(context,stringBuilder2.toString(), newFile);
这是您需要做的:
Uri fileURI = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".fileprovider", file);
“这是文件路径文件”--该文件在项目中的具体位置?raw/xml/filepath.xmlin权限我有.provider,附加到应用程序包中,但在调用getUriForFile时,我没有附加.provider。也许是这样,我现在正在测试是的,这就是问题所在谢谢你为我工作。首先我是这样做的。android:authorities=“${applicationId}.fileprovider现在我将其更改为com.memecreator.fileprovider我的实际包名与@Rayyan一致,最好使用android:authorities=“${applicationId}”“
始终;代码永远不会出错。为我工作。谢谢这个解决方案对我不起作用。。。在检查确保正确后,我仍然收到空指针异常。请使用内部目录中的文件提供程序共享您的共享文件代码,好吗?对我来说很好…我已经使用了最后两行,并且工作非常完美..谢谢android:resource=“@xml/ruta_fileprovider”@AnantaPrasad
Context context = PostAdapter.this.activity;
StringBuilder stringBuilder2 = new StringBuilder();
stringBuilder2.append(PostAdapter.this.activity.getPackageName());
stringBuilder2.append(".provider");
Uri uri;
uri = FileProvider.getUriForFile(context,stringBuilder2.toString(), newFile);
Uri fileURI = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".fileprovider", file);