这是我舱单的一部分:

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

这是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>

我从互联网上下载视频,并通过以下方式将其保存到内部存储器:

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

我试着这样使用它:

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

我得到以下错误:

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)

推荐答案

问题是,在 list 中我有这样一句话:

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

我打电话给getUriForFile时经过:

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

所以从"com.example.asd"变成了"com.example.asd.fileprovider",它起作用了

Android相关问答推荐

修改参数应该应用于哪些子元素?

Kotlin Gzip字符串未按预期工作

Android App Google AdMob";广告加载失败:3;带有测试ID,&q;广告加载失败:1 for My Gahad

Android手柄注射周期错误,多个模块引用一个核心模块

Modifer.Align()不适用于行/列/框中的文本.未解决的作用域实例错误

为什么它显示我的空白屏幕?

如何用帆布在喷气背包中画一个圆环?

SDK 33 的问题 - 面向 S+(版本 31 及更高版本)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

为什么我的 APK 中包含来自旧版 Android 支持库的类?

将 React Native 应用程序背景带到前台

Android - 如何使 TextInputEditText 的高度恰好为 2 行?

appcompat 依赖从何而来?

React-native 3D对象渲染

Jetpack Compose 动画的行为是什么?

在 Jetpack Compose 中重用具有重复代码的列

如何在 Jetpack Compose 中添加多个标签

在 Kotlin 中循环遍历字符串并用新行替换句号

Compose `Icons.Outlined.Star` 没有概述

设备文件资源管理器-ROOM 数据库中的数据库文件夹为空

Jetpack Compose 中的按钮上的文本未更新