FileProvider - 由于 FileNotFoundException 无法保存文件:/Download/TempFile.html(没有这样的文件或目录)

FileProvider - Can't save file due to FileNotFoundException: /Download/TempFile.html (No such file or directory)

似乎 Android N 现在需要 FileProvider,所以我正在尝试实现 FileProvider 以将文件从网络保存到本地临时位置,然后我需要读取此临时文件。

我这样做是为了设置 FileProvider:

Manifest.xml:

</application>
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>

然后我的 res/xml 文件夹中有一个 provider_paths.xml 文件,其中包含:

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

最后,这是我必须创建临时文件的 java 代码:

try {

    final File imagePath = new File(getContext().getFilesDir(), "Download");
    final File newFile = new File(imagePath, filename + "." + filePrefix);

    final Uri contentUri = FileProvider.getUriForFile(getContext(), getContext().getApplicationContext().getPackageName() + ".provider", newFile);

    final File tempFile = new File(contentUri.getPath());

    tempFile.getParentFile().mkdirs();
    final FileWriter writer = new FileWriter(tempFile);
    writer.flush();
    writer.close();
    return tempFile;
} catch (IOException e) {
    e.printStackTrace();
    return null;
}

final FileWriter writer = new FileWriter(tempFile);行抛出异常java.io.FileNotFoundException: /Download/TempFile.html (No such file or directory)

对我做错了什么有什么建议吗?谢谢!

Update/Edit:

当前保存文件的方法将文件放在这里: /storage/emulated/0/Download/TempFile.html

这很好,直到我尝试使用 Intent 来使用它,如下所示:

final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), fileType.getMimeType());
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

然后抛出异常:

android.os.FileUriExposedException:file:///storage/emulated/0/Download/TempFile.html exposed beyond app through Intent.getData()

Seems like FileProvider is now required for Android N

它将被广泛使用,但仅用于向其他应用程序提供内容。您的需求似乎不包括向其他应用程序提供内容。

I'm trying to implement FileProvider to save a file from the network into a local temp location

您的问题中没有任何与网络有关的代码。

Any suggestions on what I'm doing wrong?

Uri 上调用 getPath() 通常没有用。充其量,如果 Uri 的方案是 file,它可能会有用。 FileProvider 专门设计用于 而不是 为您提供带有 file 方案的 Uri,而是 content 方案。 Uri 的路径不会直接表示设备上的文件,正如 /questions/39296553/fileprovider-cant-save-file-due-to-filenotfoundexception-download-tempfile(本网页 URL 的路径)表示您的文件的路径计算机。

除此之外,您不需要 FileProvider 来制作临时文件,您确实需要 FileProvider 通过网络下载内容。


更新(基于问题更新)

首先,在您的 Intent 逻辑中将 Uri.fromFile(file) 替换为 FileProvider.getUriForFile(file)

其次,如果您确实将文件存储在 /storage/emulated/0/Download/TempFile.html 中,则需要在 FileProvider 配置中使用 external-path,而不是 files-pathfiles-path 如果您将下载的文件存储在 getFilesDir() 中。您的 /storage/emulated/0/Download/TempFile.html 路径似乎偏离了 Environment.getExternalStorageDirectory().