android - 在 onResponse 中调用 createNewFile 时,OkHttp 返回 IOException
问题描述
我正在尝试在 Android 数据的文件夹中下载 PDF。但是当我调用 createNewFile 方法时,应用程序崩溃并出现 IOException。
D/OkHttp: Callback failure for call to https://www...
D/OkHttp: java.io.IOException: No such file or directory
D/OkHttp: at java.io.UnixFileSystem.createFileExclusively0(Native Method)
D/OkHttp: at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
D/OkHttp: at java.io.File.createNewFile(File.java:1008)
D/OkHttp: at com.meidio.common.manager.FileManager$2.onResponse(FileManager.java:118)
D/OkHttp: at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:504)
D/OkHttp: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
D/OkHttp: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
D/OkHttp: at java.lang.Thread.run(Thread.java:919)
我在 onResponse 上执行下一个:
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
if (response.isSuccessful()) {
String contentType = response.body().contentType().toString();
File folder = new File(Environment.getExternalStorageDirectory(), "/Meidio");
if (!folder.exists()) {
boolean folderCreated = folder.mkdir();
}
File file = new File(folder.getPath() + "/document.pdf");
if (file.exists()) {
boolean fileDeleted = file.delete();
LoggerManager.handlesDebug("openPdfFromUrl", "File deleted: " + fileDeleted + "");
}
//Create New File if not present
boolean fileCreated = false;
if (!file.exists()) {
file.getParentFile().mkdirs();
fileCreated = file.createNewFile();
}
Uri uriCreated;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uriCreated = FileProvider.getUriForFile(MDApplication.getCurrentAppContext().getApplicationContext(), MDApplication.getCurrentAppContext().getApplicationContext().getPackageName() + ".provider", file);
} else {
uriCreated = Uri.fromFile(file);
}
BufferedSink sink = Okio.buffer(Okio.sink(file));
sink.writeAll(response.body().source());
sink.close();
if (fileCreated) {
Intent target = new Intent(Intent.ACTION_VIEW);
target.setData(uriCreated);
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
context.startActivity(target);
BusProvider.getInstance().post(new ShowHideLoadingEvent(false, false)); // Loading Dismiss
LoggerManager.handlesDebug("openPdfFromUrl", "Response OK");
} else {
LoggerManager.handlesDebug("openPdfFromUrl", "Response OK, but file not created!");
}
}
提供程序路径 xml 具有以下内容:
<?xml version="1.0" encoding="utf-8"?><!--
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="files_root"
path="Android/data/${applicationId}" />
<external-path
name="external_files"
path="." />
<external-path
name="/storage/emulated/0"
path="." />
<external-files-path
name="documents"
path="/Meidio" />
<files-path
name="app_contact_photos"
path="app_contact_photos" />
<cache-path name="cache" path="." />
<external-path name="external" path="." />
<root-path name="root" path="." />
<files-path name="contact_photos" path="/" />
<files-path name="contact_photos" path="app_contact_photos/"/>
<files-path name="files" path="." />
<external-cache-path name="external_cache" path="." />
<external-cache-path
name="cache"
path="camera" />
</paths>
我不知道是什么错误,试图创建的文件是下一个/storage/emulated/0/meidio/document.pdf。所以,我有点迷路了。
一些忠告?
解决方案
推荐阅读
- javascript - BigNumber.js 简单的减法给出错误的输出
- ios - XCODE:标签约束表现得很奇怪
- java - 如何从 txt 文件中读取单独的部分以在 Java GUI 中显示?
- python - 如何基于多级索引计算时间戳的差异?
- flutter - Flutter 以编程方式设置 InputDatePickerFormField 的日期
- php - 如何在 [ ] 之间获取字符串
- r - 从具有超过(某些点)的值的数据集中创建数据框
- c++ - OpenGL绘制多个顶点数组
- react-native - 避免在底部的项目上使用键盘 - React Native
- .htaccess - htaccess 301 重定向到 root 并删除查询字符串