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

  • 相关阅读:
    Axios 各种请求方式传递参数格式
    axios POST提交数据的三种请求方式写法
    Json对象和Json字符串的区别
    ASP.NET Core 使用 AutoFac 注入 DbContext
    asp.net core signalr Error: Failed to start the transport 'WebSockets': null
    js中settimeout和setinterval的区别是什么?
    VS IDE开发字体推荐
    .net core ef core 自动迁移,自动修改数据库
    localstorage和sessionstorage的区别
    TinyOS编程
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/6067288.html
Copyright © 2011-2022 走看看