我正在尝试创建一个下载pdf并保存它的函数,我正在使用以下代码。
public class DownloadFile extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
String fileUrl = strings[0]; // -> http://maven.apache.org/maven-1.x/maven.pdf
String fileName = strings[1]; // -> maven.pdf
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "testthreepdf");
folder.mkdir();
File pdfFile = new File(folder, fileName);
try{
pdfFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
FileDownloader.downloadFile(fileUrl, pdfFile);
return null;
}
}
大约60%的时间都可以正常工作,但有时会在线上:
File pdfFile = new File(folder, fileName);
它崩溃了应用程序,当我再次尝试它然后工作。它抛出的错误并没有那么有用
执行doInBackground()
堆栈跟踪:
08-17 15:08:48.097 27281-27317/com.heyjude.heyjudeapp W/System.err: mkdir failed: EEXIST (File exists) : /storage/emulated/0/testthreepdf
08-17 15:09:36.807 27281-27311/com.heyjude.heyjudeapp I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-17 15:09:36.847 27281-27311/com.heyjude.heyjudeapp I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
08-17 15:09:36.877 27281-27311/com.heyjude.heyjudeapp I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false