java - 某些手机无法从 android 中的 url(POST) 下载 pdf
问题描述
在此处输入图像描述从流中下载文件(url respunce)我尝试不使用移动设备。但某些移动设备无法使用,文件创建时未写入。我正在更改字节[4096],字节[1024] ...,发生同样的错误请给我任何建议
public void saveToDisk(ResponseBody body, String pdfFileName, String module_type, String test_name) {
try {
File sdcard_destinationFile;
if (module_type.equalsIgnoreCase("ADD")) {
sdcard_destinationFile = new File(Environment.getExternalStorageDirectory()
+ "/download/" + pdfFileName + "_" + test_name + ".pdf");
} else {
sdcard_destinationFile = new File(Environment.getExternalStorageDirectory()
+ "/download/" + pdfFileName + ".pdf");
}
InputStream is = null;
OutputStream os = null;
try {
Log.e("file",body+"");
is = body.byteStream();
os = new FileOutputStream(sdcard_destinationFile);
byte data[] = new byte[4096];
int count;
int progress = 0;
while ((count = is.read(data)) != -1) {
os.write(data, 0, count);
progress += count;
Log.d(TAG, "Progress: " + progress + "/" + body.contentLength() + " >>>> " + (float) progress / body.contentLength());
}
Log.d("aaaaa", "Progress: " + progress + "/" + body.contentLength() + " >>>> " + (float) progress / body.contentLength());
os.flush();
Log.e("isis",is +"");
Log.e("isis",os +"");
if (is != null) is.close();
if (os != null) os.close();
Log.d(TAG, "File saved successfully!");
showPdf(pdfFileName, module_type, test_name);
return;
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "Failed to save the file!");
return;
} finally {
}
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "Failed to save the file!");
return;
}
}
解决方案
推荐阅读
- javascript - 信用卡验证 - 带有空格的匹配模式
- css - Swiper js 不会自动调整 swiper-container 和 swiper-slide 类的大小
- c++ - 链接 GLFW 文件时出错
- flutter - 我可以在我的 Flutter 应用程序中使用其他一些 Youtube 视频吗
- python - add_finished_callback() 中没有事务 - 如何重新启动?
- javascript - 如何按 AM/PM 时间属性订购此 JSON 响应?
- javascript - 使用递归javascript反转字符串
- python - ModuleNotFoundError:没有名为“praw”的模块
- android - Android Studio , startService() 每次调用 stopService() 和 startService() 时都会创建对象
- python-3.x - UnexpectedTagNameException:消息:选择仅适用于