android – java.io.IOException:BufferedInputStream已关闭

我正在尝试从android中的listview加载来自url的图像.我正在使用下面的代码来做到这一点.

        Bitmap bm;
        try {
            URL url = new URL(imageUrl);
            bm = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        imageView.setImageBitmap(bm);

我正在AsyncTask中执行上面的代码.当我执行时,它显示正确.但是在我转移到另一个活动并返回后,图像没有显示.那时我的logcat显示,

W/System.err(3570): java.io.IOException: BufferedInputStream is closed
W/System.err(3570):     at java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:116)
W/System.err(3570):     at java.io.BufferedInputStream.read(BufferedInputStream.java:274)
W/System.err(3570):     at org.apache.harmony.luni.internal.net.www.protocol.http.UnknownLengthHttpInputStream.read(UnknownLengthHttpInputStream.java:40)
W/System.err(3570):     at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:166)
W/System.err(3570):     at java.io.BufferedInputStream.read(BufferedInputStream.java:324)
W/System.err(3570):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
W/System.err(3570):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:573)
W/System.err(3570):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:628)
W/System.err(3570):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
W/System.err(3570):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
W/System.err(3570):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
W/System.err(3570):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
W/System.err(3570):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
W/System.err(3570):     at java.lang.Thread.run(Thread.java:1019)

实际上错误显示在下面的行,

bm = BitmapFactory.decodeStream(url.openConnection().getInputStream());

我搜索并在SO中找到与此相关的不同帖子,但找不到任何解决方案……
谁能帮我吗?

最佳答案
尝试来自Google的Jess的解决方法:

要修复的代码:

/**
 * This input stream won't read() after the underlying stream is exhausted.
 * http://code.google.com/p/android/issues/detail?id=14562
 */
final class DoneHandlerInputStream extends FilterInputStream {
    private boolean done;

    public DoneHandlerInputStream(InputStream stream) {
        super(stream);
    }

    @Override public int read(byte[] bytes, int offset, int count) throws IOException {
        if (!done) {
            int result = super.read(bytes, offset, count);
            if (result != -1) {
                return result;
            }
        }
        done = true;
        return -1;
    }
}

要使用,请在DoneHandlerInputStream中包装从getInputStream()返回的流:

   InputStream stream = url.openConnection().getInputStream();
   stream = new DoneHandlerInputStream(stream);

   bm = BitmapFactory.decodeStream(stream);
   //rest of your code

请参阅:
http://code.google.com/p/android/issues/detail?id=14562

转载注明原文:android – java.io.IOException:BufferedInputStream已关闭 - 代码日志