Java 文件提供者-Can';由于FileNotFoundException:/Download/TempFile.html(没有这样的文件或目录),无法保存文件
看起来Android N现在需要FileProvider,所以我正在尝试实现FileProvider,将一个文件从网络保存到本地临时位置,然后我需要读取这个临时文件 我这样做是为了设置文件提供程序: Manifest.xml:Java 文件提供者-Can';由于FileNotFoundException:/Download/TempFile.html(没有这样的文件或目录),无法保存文件,java,android,android-fileprovider,Java,Android,Android Fileprovider,看起来Android N现在需要FileProvider,所以我正在尝试实现FileProvider,将一个文件从网络保存到本地临时位置,然后我需要读取这个临时文件 我这样做是为了设置文件提供程序: Manifest.xml: </application> <provider android:name="android.support.v4.content.FileProvider" android:authorities="${appl
</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>
最后,这是我必须创建临时文件的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=newfilewriter(tempFile)的行
抛出异常java.io.FileNotFoundException:/Download/TempFile.html(没有这样的文件或目录)
对我做错了什么有什么建议吗?谢谢大家!
更新/编辑:
当前保存文件的方法将文件放置在此处:
/storage/emulated/0/Download/TempFile.html
这很好,直到我有目的地消费它,就像这样:
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 通过Intent.getData()在应用程序之外公开
似乎Android N现在需要FileProvider
它将被广泛使用,但仅用于向其他应用程序提供内容。您的需求似乎不包括将内容提供给其他应用程序
我正在尝试实现FileProvider,以便将文件从网络保存到本地临时位置
您的问题中没有与网络有关的代码
对我做错了什么有什么建议吗
在Uri
上调用getPath()
通常是无用的。充其量,如果Uri
的方案是文件
,那么它可能很有用FileProvider
专门设计为不提供带有文件
方案的Uri
,而是提供内容
方案。该Uri
的路径不会直接表示设备上的文件,正如/questions/39296553/fileprovider无法保存文件一样,因为filenotfoundexception download tempfile
(此网页的URL路径)表示计算机上的文件路径
除此之外,制作临时文件不需要文件提供商
,通过网络下载内容也需要文件提供商
更新(基于问题更新)
首先,将Intent
逻辑中的Uri.fromFile(file)
替换为FileProvider.getUriForFile(file)
其次,如果确实要将文件存储在/storage/emulated/0/Download/TempFile.html
中,则需要在文件提供程序
配置中使用外部路径
,而不是文件路径
<如果将下载的文件存储在getFilesDir()
中,则代码>文件路径将为。您的/storage/emulated/0/Download/TempFile.html
路径似乎已脱离环境。getExternalStorageDirectory()
实际上,我试图解决的问题是:android.os.FileUriExposedException:file:///storage/emulated/0/Download/TempFile.html 通过Intent.getData()在应用程序之外公开解决这个问题的办法是使用FileProvider,对吗?我甚至发现了一些你不得不在谷歌上搜索的问题,而这似乎是安卓N,targetSdk 24中的一个强制性问题。创建该文件时,我将向try块传递一个内容,并将其删除。我需要做的是创建一个文件,并将其保存在本地。@TooManyEduardos:“解决方法是使用FileProvider,对吗?”--是的,但仅适用于触发该异常的任何内容。问题中的代码中没有任何东西会导致这种情况,因为您没有调用其他应用程序。通过FileProvider
将文件正常下载到您正在服务的某个位置。当您尝试将内容提供给其他应用程序时,请使用FileProvider.getUriForFile()
。演示这一点,其中我使用getUriForFile()
仅用于通知
。“newFile”是:“/data/user/0/com.myapp.android.apps.myapp/files/Download/TempFile.html”,而“contentUri”是”content://com.myapp.android.apps.myapp.provider/Download/TempFile.html"所以这也是非常重要的odd@TooManyEduardos:没有,这是意料之中的。您可能希望阅读。@ToomanyDuardos:使用一个setFlags()
或使用addFlags()
。第二个setFlags()
将清除第一个setFlags()
。
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), fileType.getMimeType());
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);