为什么这个位图图像没有Android的下载? [英] Why is this image bitmap not downloading in Android?

查看:122
本文介绍了为什么这个位图图像没有Android的下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我有从一个URL下载图像的appliation。我使用下面的code为相同:

I have an appliation in which I have to download an image from an URL. I am using the following code for the same:

URL url = new URL(address);
URLConnection conn = url.openConnection();
conn.connect();
int length = conn.getContentLength();
is = conn.getInputStream();
bis = new BufferedInputStream(is, length);
bm = BitmapFactory.decodeStream(bis);

被返回由于某种原因在BM具有高度和宽度-1,这是throwning非法状态异常。什么可能是高度和宽度来了-1的原因是什么?

The bm which is returned for some reason has height and width -1, and this is throwning Illegal state exception. What could be the reason that height and width is coming -1?

推荐答案

请看看下面code

String url = server url;
InputStream ins = null;
    try {
        ins = new java.net.URL(url).openStream();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Bitmap b = BitmapFactory.decodeStream(new FlushedInputStream(ins));
imageview.setImageBitmap(b);

和以下功能也使用

 static class FlushedInputStream extends FilterInputStream {
    public FlushedInputStream(InputStream inputStream) {
        super(inputStream);
    }

    @Override
    public long skip(long n) throws IOException {
        long totalBytesSkipped = 0L;
        while (totalBytesSkipped < n) {
            long bytesSkipped = in.skip(n - totalBytesSkipped);
            if (bytesSkipped == 0L) {
                int b = read();
                if (b < 0) {
                    break; // we reached EOF
                } else {
                    bytesSkipped = 1; // we read one byte
                }
            }
            totalBytesSkipped += bytesSkipped;
        }
        return totalBytesSkipped;
    }
}

这篇关于为什么这个位图图像没有Android的下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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