IOException:流关闭,BitmapFactory decodeStream

时间:2014-08-12 23:05:34

标签: java android asynchronous inputstream

我从一个API获得了一个图片流。我得到了doInBackground of the Asynctask中的流,我没有遇到崩溃,NullPointerException反对InputStream。但是当我尝试使用它来构建位图时,它总是返回stream is closed并且我根本不关闭它。

private class Worker extends Asynctask<.....> {

    private StreamSource streamSource;
    private Bitmap bmp;

    @Override
    public void doInBackground (.....) {
        try {
            streamSource = gallery.read ();

            bmp = BitmapFactory.decodeStream (streamSource.getStream ()); // HERE ! :@

        } catch (Exception e) {
            e.printStackTrace ();
        }           
    }

    @Override
    public void onPostExecute (.....) {
        imageView.setImageBitmap (bmp);
    }
}

这是我得到的例外:

08-12 22:33:00.973: W/System.err(20359): java.io.IOException: stream closed
08-12 22:33:00.973: W/System.err(20359):    at com.android.okhttp.internal.http.AbstractHttpInputStream.checkNotClosed(AbstractHttpInputStream.java:68)
08-12 22:33:00.973: W/System.err(20359):    at com.android.okhttp.internal.http.HttpTransport$ChunkedInputStream.read(HttpTransport.java:430)
08-12 22:33:00.973: W/System.err(20359):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-12 22:33:00.973: W/System.err(20359):    at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:620)
08-12 22:33:00.973: W/System.err(20359):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
08-12 22:33:00.973: W/System.err(20359):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:634)
08-12 22:33:00.973: W/System.err(20359):    at com.package.activities.MediasActivity$Worker.doInBackground(MediasActivity.java:158)
08-12 22:33:00.973: W/System.err(20359):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-12 22:33:00.973: W/System.err(20359):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-12 22:33:00.973: W/System.err(20359):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-12 22:33:00.973: W/System.err(20359):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-12 22:33:00.973: W/System.err(20359):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-12 22:33:00.973: W/System.err(20359):    at java.lang.Thread.run(Thread.java:841)
08-12 22:33:00.973: D/skia(20359): ---- read threw an exception
08-12 22:33:00.973: D/skia(20359): --- decoder->decode returned false

抛出异常:BitmapFactory.decodeStream

我可以保证我没有关闭流,如果我愿意这样做我会做的:streamSource.close ();但我没有。

你们有没有想过这可能是什么原因?谢谢。

2 个答案:

答案 0 :(得分:0)

BitmapFactory.decodeStream很可能在完成后关闭流。如果您在AsyncTask工作者之间共享streamSource,那么可能正在关闭一个流而另一个正在尝试从中读取。

答案 1 :(得分:0)

您是否尝试使用runOnUiThread解码流?例如写

runOnUiThread(new Runnable(){
        public void run() {
            streamSource = gallery.read ();

            bmp = BitmapFactory.decodeStream (streamSource.getStream ()); // HERE ! :@

        }
    });