Oma*_*mar 27 java fileoutputstream
我正在尝试从互联网上下载一个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;
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
/mnt/sdcard/tmp/3.png:打开失败:EISDIR(是一个目录)
为什么是这样?
"/ mnt/sdcard/tmp /"存在.
Dir*_*irk 82
3.png是一个目录,因为你通过调用来实现f.mkdirs();.试试吧f.getParentFile().mkdirs().从文档:
创建此抽象路径名指定的目录,包括任何必需但不存在的父目录.请注意,如果此操作失败,则可能已成功创建一些必需的父目录.
(强调我的).换句话说,File实例中包含的整个路径f被视为目录名称,直到并包括最终部分(3.png在示例输出中).
Led*_*ine 14
问题是您正在使用该功能
f.mkdirs();
Run Code Online (Sandbox Code Playgroud)
此函数将创建一个名为"3.png"的文件夹,而不是名为"3.png"的文件,因此请先删除此文件夹,

然后更换功能
f.mkdirs();
Run Code Online (Sandbox Code Playgroud)
至
f.createNewFile();
Run Code Online (Sandbox Code Playgroud)
希望这有帮助.
| 归档时间: |
|
| 查看次数: |
40114 次 |
| 最近记录: |