java.io.IOException:在android java中下载图像文件时,BufferedInputStream被关闭

时间:2013-05-10 08:20:40

标签: java android inputstream httpconnection

我收到以下错误

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)

在这行代码中:

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

OpenHttpConnection()函数是:

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

1 个答案:

答案 0 :(得分:0)

您忽略了OpenConnection()方法可能的null返回。允许该方法只抛出其声明的异常而不是在内部捕获它们会更好。