从互联网上加载的图片无法正常显示?

时间:2012-01-20 05:12:49

标签: android url imageview

我正在从互联网上获取的图像视图中显示图像。问题是它不会一直显示如果我查看该页面5次只显示图像的3或4次(每次我从互联网上下载图像)如何每次都显示它..

public Bitmap getDrawable(String url) throws MalformedURLException, IOException {
        Bitmap x;

        HttpURLConnection connection = (HttpURLConnection)new URL(url) .openConnection();

        connection.connect();
        InputStream input = connection.getInputStream();

        x = BitmapFactory.decodeStream(input);
        return x;
    }

for (int i = 0; i < imageSourceArray.length - 1; i++) {
        detailedArticleImageViewArray[i] = new ImageView(
            ArticleActivity.this);
        System.out.println(TMI + imageSourceArray[i + 1]);
        Bitmap image = getDrawable(TMI + imageSourceArray[i + 1]);
        detailedArticleImageViewArray[i].setImageBitmap(image);
        detailedArticleImageViewArray[i].setLayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT, 250));
        detailedArticleImageViewArray[0].setPadding(5, 10, 10, 5);
                }

我收到IOException Buffered InputStream已关闭...

另一个原因是解码返回null或false ...

提前致谢..

2 个答案:

答案 0 :(得分:0)

如果您尝试从网址显示图片,请使用此功能。

Bitmap mbmp = BitmapFactory.decodeStream(new java.net.URL("urlname").openStream());
Imageview_ref.setImageBitmap(mbmp);

我认为没有必要每次下载以显示图像。

答案 1 :(得分:0)

使用此课程

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;
    }
}