【问题标题】:FileProvider - Can't save file due to FileNotFoundException: /Download/TempFile.html (No such file or directory)FileProvider - 由于 FileNotFoundException 无法保存文件:/Download/TempFile.html(没有这样的文件或目录)
【发布时间】:2016-09-02 16:00:24
【问题描述】:

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)

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

更新/编辑:

当前保存文件的方法将文件放置在此处: /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()

【问题讨论】:

    标签: java android android-fileprovider


    【解决方案1】:

    Android N 现在似乎需要 FileProvider

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

    我正在尝试实现 FileProvider 以将文件从网络保存到本地临时位置

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

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

    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-path。如果您将下载的文件存储在getFilesDir() 中,files-path 将是。您的 /storage/emulated/0/Download/TempFile.html 路径似乎与 Environment.getExternalStorageDirectory() 不同。

    【讨论】:

    • 实际上,我要解决的是: android.os.FileUriExposedException: file:///storage/emulated/0/Download/TempFile.html 通过 Intent.getData 暴露在应用程序之外() 解决方法是使用 FileProvider,对吗?我什至发现了一些你必须用谷歌搜索的问题,这似乎是现在在 Android N,targetSdk 24 中的强制性内容。该文件是使用我传递给 try 块的内容创建的,我删除了它这个问题。我需要做的是创建一个文件,并将其保存在本地。
    • @TooManyEduardos:“解决这个问题的方法是使用 FileProvider,对吧?” - 是的,但仅适用于触发该异常的任何内容。您的问题中的代码中没有任何内容会导致这种情况,因为您没有调用其他应用程序。将文件正常下载到您通过FileProvider 提供服务的某个位置。当您尝试将内容提供给其他应用程序时,请使用FileProvider.getUriForFile()This sample app 演示了这一点,我将 getUriForFile() 仅用于 Notification
    • “newFile”为:“/data/user/0/com.myapp.android.apps.myapp/files/Download/TempFile.html”,“contentUri”为“content: //com.myapp.android.apps.myapp.provider/Download/TempFile.html" 所以这也很奇怪
    • @TooManyEduardos:不,这是意料之中的。您不妨阅读the documentation for FileProvider
    • @TooManyEduardos:使用setFlags()addFlags() 之一。你的第二个 setFlags() 会消灭你的第一个 setFlags()
    猜你喜欢
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    相关资源
    最近更新 更多