Java.io.IOException: BufferedInputStream closed

I am trying to upload images from url to listview in android. For this, I use the code below.

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

I am executing the above code in AsyncTask. When I execute, it displays correctly. But after I move on to another action and return, the image is not displayed. At that time, my logarithm shows

 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) 

In fact, the error appears in the line below,

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

I searched and found various posts related to this in SO, but could not find a solution ... Can someone help me?

+4
source share
4 answers

try this jess workaround from google:

for correction:

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

To use, wrap the stream returned from getInputStream () in DoneHandlerInputStream:

  InputStream stream = url.openConnection().getInputStream(); stream = new DoneHandlerInputStream(stream); bm = BitmapFactory.decodeStream(stream); //rest of your code 

see full explanation at: http://code.google.com/p/android/issues/detail?id=14562

+2
source

Try

 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); 
0
source
  try { is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); bm = BitmapFactory.decodeStream(bis); is.close();//add conn.disconnect();//add } catch (IOException e) { e.printStackTrace(); } 
0
source

You can access http s endpoint using http url

0
source

All Articles