FileOutputStream上的文件非文件异常-从移动应用程序下载文件-android
我试图为用户提供从android应用程序下载.xls文件的功能。正在从服务器发送.xls文件。它在FileOutputStream处显示文件未找到异常。 我的代码片段:FileOutputStream上的文件非文件异常-从移动应用程序下载文件-android,android,android-studio,file-io,ioexception,fileoutputstream,Android,Android Studio,File Io,Ioexception,Fileoutputstream,我试图为用户提供从android应用程序下载.xls文件的功能。正在从服务器发送.xls文件。它在FileOutputStream处显示文件未找到异常。 我的代码片段: protected Boolean doInBackground(String... params) { // params is an array where params[0]= url to download the .xls file and params[1] = filename
protected Boolean doInBackground(String... params) {
// params is an array where params[0]= url to download the .xls file and params[1] = filename
File dir = null;
try {
if(isSDCardPresent()) {
dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Report");
if (!dir.exists()) {
if(dir.mkdirs()){
URL url = new URL(params[0]);
File reportFile = new File(dir, params[1]);
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
//We create an array of bytes
byte[] data = new byte[50];
int current = 0;
while ((current = bis.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, current);
}
FileOutputStream fos = new FileOutputStream(reportFile);
fos.write(buffer.toByteArray());
fos.close();
}
else {
Toast.makeText(getApplicationContext(),"Unable to download",Toast.LENGTH_SHORT).show();
}
}
}else{
Toast.makeText(getApplicationContext(),"no sd card",Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
找不到哪个文件?请输入完整路径。@blackapps reportFile
如果(!dir.exists()){dir.mkdirs();}
您应该检查mkdirs()的返回值。它可能会失败,如果失败,它将返回false。在这种情况下,向这样说的用户敬酒。并返回,因为如果没有目录,继续是没有意义的。reportFile
Full path请reportFile.getAbsolutePath()
。如果sd卡不存在。。。你敬酒。好啊但是不要继续下去,因为那没有意义。