zoukankan      html  css  js  c++  java
  • android- 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

  • 相关阅读:
    集群服务器登录退出出现问题
    TP框架中的Db::name 和 dB::table 以及 db('') 的区别
    TP5中orderRaw用法
    视差滚动 插件
    ThinkPad t480s 电源接口进水了
    file_put_contents failed to open stream: Permission denied in
    Mac 如何安装字体?
    ZipArchive::close(): Failure to create temporary file: Permission denied
    Wifi6 路由器推荐
    名词解释 | Enteric Nervous System | Enteric Neural Crest Cell | ENS | ENCC | 神经系统 | 神经嵴细胞
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/6067288.html
Copyright © 2011-2022 走看看