FileProvider崩溃 - NPE试图调用一个空字符串XmlResourceParser [英] FileProvider crash - npe attempting to invoke XmlResourceParser on a null String

查看:7698
本文介绍了FileProvider崩溃 - NPE试图调用一个空字符串XmlResourceParser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的清单中的一部分:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.asd
    安卓版code =118
    机器人:=的versionName118>    <用途-SDK
        安卓的minSdkVersion =14
        机器人:targetSdkVersion =19/>
    <应用
        机器人:名字=com.example.asd.AsdApplication
        机器人:allowBackup =真
        机器人:allowTaskReparenting =真
        机器人:主题=@风格/ AsdTheme>
        ...        <供应商
            机器人:名字=com.example.asd.database.hq.ContentProviderDB
            机器人:当局=ourContentProviderAuthorities>
        < /提供商GT;
        <供应商
            机器人:名字=android.support.v4.content.FileProvider
            机器人:当局=com.example.asd.fileprovider
            机器人:出口=假
            机器人:grantUriPermissions =真正的>
            &所述;元数据
                机器人:名字=android.support.FILE_PROVIDER_PATHS
                机器人:资源=@ XML /文件路径/>
        < /提供商GT;       ...
    < /用途>< /清单>

这是原/ XML / filepaths.xml的文件路径文件

 <?XML版本=1.0编码=UTF-8&GT?;
<路径的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <文件路径名=媒体/>
< /路径>

我下载从互联网上的视频,并将其保存到内部存储是这样的:

 公共静态布尔saveInputStreamToInternalStorageFile(上下文的背景下,字符串的文件名,字节[] dataToWrite,上下文CTX){
    FileOutputStream中FOS;
    尝试{
        FOS =新的FileOutputStream(context.getFilesDir()+文件分割符+文件名);        ObjectOutputStream的OOS =新的ObjectOutputStream(FOS);
        oos.writeObject(dataToWrite);
        oos.close();
        返回true;
    }赶上(FileNotFoundException异常五){
        e.printStackTrace();
        返回false;
    }赶上(IOException异常五){
        e.printStackTrace();
        返回false;
    }
}

我尝试使用它像这样:

 私人无效playVideoFromDeviceWithWorkaround(字符串文件名){    文件NEWFILE =新的文件(getFilesDir(),文件名);
    乌里contentUri = FileProvider.getUriForFile(getApplicationContext(),com.example.asd,NEWFILE);    尝试{
        vvVideoFullscreen.setVideoURI(contentUri);
        showMediaControls = TRUE;
        的playVideo();
    }赶上(例外五){
        playVideoFromNetwork();
    }}

在这一行:

 乌里contentUri = FileProvider.getUriForFile(getApplicationContext(),com.example.asd,NEWFILE);

我收到以下错误:

 显示java.lang.NullPointerException:尝试调用虚拟方法android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,JAVA在一个空对象引用.lang.String)
在android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:560)
在android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
在android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)


解决方案

但问题是,在清单我有这行:

 的android:当局=com.example.asd.fileprovider

和调用getUriForFile当我经过:

 乌里contentUri = FileProvider.getUriForFile(getApplicationContext(),com.example.asd,NEWFILE);

因此​​,从变com.example.asdcom.example.asd.fileprovider和它的工作

This is a part of my manifest:

<?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>

This is the filepaths file in raw/xml/filepaths.xml

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

I download a video from internet and save it to internal storage this way:

public static boolean saveInputStreamToInternalStorageFile(Context context, String filename, byte[] dataToWrite, Context ctx) {
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(context.getFilesDir() + File.separator + filename);

        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(dataToWrite);
        oos.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

I try to use it like so:

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();
    }

}

At this line:

Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile); 

I get the following error:

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)

解决方案

The problem was that in Manifest I had this line:

android:authorities="com.example.asd.fileprovider"

and when calling getUriForFile I was passing:

Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile); 

So changed from "com.example.asd" to "com.example.asd.fileprovider" and it worked

这篇关于FileProvider崩溃 - NPE试图调用一个空字符串XmlResourceParser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆