文件输出流崩溃,下载映像时出现“打开失败:EISDIR(是目录)”错误
2022-09-01 06:19:48
我正在尝试从互联网上下载一个iamge,这是代码:
try {
String imgURL = c.imgURL;
String imgPATH = c.imgPATH;
URL url = new URL(imgURL);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
try {
File f = new File(imgPATH);
f.mkdirs();
BufferedInputStream input = new BufferedInputStream(url.openStream());
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(imgPATH), 8192); // CRASH HERE
byte data[] = new byte[8192];
long total = 0;
int count = 0;
int updateUILimiter = 0;
while ((count = input.read(data)) != -1) {
total += count;
if (updateUILimiter == 20)
// publishProgress((int) (total * 100 / lenghtOfFile));
updateUILimiter = 0;
else
updateUILimiter++;
output.write(data, 0, count);
if (isCancelled()) {
output.flush();
output.close();
input.close();
return null;
}
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
c.imgPATH = "";
return null;
}
} catch (Exception e) {
c.imgPATH = "";
return null;
}
下面是错误消息:
/mnt/sdcard/tmp/3.png: 打开失败: EISDIR (是一个目录)
这是为什么呢?
“/mnt/sdcard/tmp/”存在。