java.io.IOException:BufferedInputStream 已关闭
Posted
技术标签:
【中文标题】java.io.IOException:BufferedInputStream 已关闭【英文标题】:java.io.IOException: BufferedInputStream is closed 【发布时间】:2013-02-26 16:56:10 【问题描述】:我正在尝试在 android 的列表视图中从 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 中搜索并找到了与此相关的不同帖子,但找不到任何解决方案... 谁能帮帮我?
【问题讨论】:
【参考方案1】:试试 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;
要使用,将从 getInputStream() 返回的流包装在 DoneHandlerInputStream 中:
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
【讨论】:
【参考方案2】:试试
URL url = new URL(url);
private Bitmap getBitmap(String url)
try
Bitmap bm = null;
bm = BitmapFactory.decodeStream(url.openConnection().getInputStream());
return bitmap;
catch (Exception e)
Log.e("Error", e.getMessage());
e.printStackTrace();
imageView.setImageBitmap(getBitmap.url);
【讨论】:
【参考方案3】: try
is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
is.close();//add
conn.disconnect();//add
catch (IOException e)
e.printStackTrace();
【讨论】:
【参考方案4】:您可能正在使用 http url
访问 https 端点【讨论】:
以上是关于java.io.IOException:BufferedInputStream 已关闭的主要内容,如果未能解决你的问题,请参考以下文章
java.io.IOException: toDerInputStream 拒绝标签类型 77
java.io.IOException:系统找不到指定的路径
java.io.IOException: Connection reset by peer和java.io.IOException: Connection timed out。Socket
android java.io.IOException:传输端点未连接