java.io.IOException: BufferedInputStream is closed when downloading image file in android java

StackOverflow https://stackoverflow.com/questions/16477903

سؤال

I got hte following error

05-10 11:14:47.925: W/System.err(9681): java.io.IOException: BufferedInputStream is closed
05-10 11:14:47.925: W/System.err(9681):     at java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:118)
05-10 11:14:47.925: W/System.err(9681):     at java.io.BufferedInputStream.available(BufferedInputStream.java:112)
05-10 11:14:47.925: W/System.err(9681):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
05-10 11:14:47.925: W/System.err(9681):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
05-10 11:14:47.925: W/System.err(9681):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:601)
05-10 11:14:47.925: W/System.err(9681):     at com.laroche.NewsMedia.getImageBitmap(NewsMedia.java:132)
05-10 11:14:47.932: W/System.err(9681):     at com.laroche.News.getNewsListByCategoryId(News.java:113)
05-10 11:14:47.932: W/System.err(9681):     at com.laroche.MainActivity$GetDataTask.doInBackground(MainActivity.java:112)
05-10 11:14:47.932: W/System.err(9681):     at com.laroche.MainActivity$GetDataTask.doInBackground(MainActivity.java:1)
05-10 11:14:47.932: W/System.err(9681):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-10 11:14:47.932: W/System.err(9681):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-10 11:14:47.932: W/System.err(9681):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-10 11:14:47.932: W/System.err(9681):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-10 11:14:47.932: W/System.err(9681):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-10 11:14:47.932: W/System.err(9681):     at java.lang.Thread.run(Thread.java:856)

in this line of code:

BufferedInputStream bufferedInputStream = null;
    Bitmap bmp = null;
    String urlString = "http://ma2too3a.com:8084/NewsImages/"+imageName;
    try {
        bufferedInputStream = new BufferedInputStream(ws.OpenHttpConnection(urlString));
        bmp = BitmapFactory.decodeStream(bufferedInputStream);

        saveImage(bmp,imageName,c);
        bufferedInputStream.close();


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

where the OpenHttpConnection() function is :

public InputStream OpenHttpConnection(String urlString) throws IOException{
    InputStream in = null;
    int respobse = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();
    if(!(conn instanceof HttpURLConnection))
        throw new IOException("It's not HTTP connection");
    try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        respobse = httpConn.getResponseCode();
        if(respobse == HttpURLConnection.HTTP_OK){
            in= httpConn.getInputStream();
            Log.d("HTTP connection","OK");
        }
    }catch (IOException e) {
        Log.e("HTTP ERROR",e.toString());
        throw new IOException();
    }
    return in;
}
هل كانت مفيدة؟

المحلول

You're ignoring the possible null return from your OpenConnection() method. It would be better to allow that method to just throw its declared exceptions and not catch them internally.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top