FileOutputStream崩溃时出现“open failed:EISDIR(Is a directory)”下载图像时出错 [英] FileOutputStream crashes with "open failed: EISDIR (Is a directory)" error when downloading image

查看:2034
本文介绍了FileOutputStream崩溃时出现“open failed:EISDIR(Is a directory)”下载图像时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从互联网上下载iamge,以下是代码:

I'm trying to download an iamge from the internet, Here is the code:

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:open failed:EISDIR(是目录)

/mnt/sdcard/tmp/3.png: open failed: EISDIR (Is a directory)

这是为什么?

/ mnt / sdcard / tmp /存在。

" /mnt/sdcard/tmp/" exists.

推荐答案

3.png 是一个目录,因为你可以通过调用 f.mkdirs(); 来实现。请尝试 f.getParentFile()。mkdirs()。来自文档

3.png is a directory, because you make it so by calling f.mkdirs();. Try f.getParentFile().mkdirs() instead. From the documentation:


创建由此抽象路径名命名的目录,包括任何必要但不存在的父目录。请注意,如果此操作失败,则可能已成功创建一些必要的父目录。

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

(强调我的)。换句话说,文件实例 f 中包含的整个路径被视为目录名,最多为包括最后一部分(示例输出中的 3.png )。

(emphasis mine). In other words, the entire path contained in the File instance f is taken to be a directory name, up to and including the final part (3.png in the example output).

这篇关于FileOutputStream崩溃时出现“open failed:EISDIR(Is a directory)”下载图像时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆