java.io.IOException:意外的流结束(Android)

时间:2013-11-01 13:52:10

标签: java android android-asynctask

在下载图像的过程中按下后退按钮(关闭活动)时遇到问题。我有一个asynctask设置来下载图像(使用Android开发人员网站中定义的过程)但是如果图像尚未完全下载我遇到了意外的流结束崩溃我点击后退按钮返回以前的活动。

这是我的asynctask和调整图像大小的方法(如果它太大):

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    RelativeLayout spinnerLayout = (RelativeLayout) findViewById(R.id.spinnerLayout);
    protected void onPreExecute() {
        spinnerLayout.setVisibility(View.VISIBLE);
    }

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];

        Bitmap mIcon11 = null;
        try {

            //decodes image size
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            in = new BufferedInputStream(new java.net.URL(urldisplay).openStream());


            BitmapFactory.decodeStream(in, null, options);

            options.inSampleSize=calculateInSampleSize(options, 512, 268);
            options.inJustDecodeBounds=false;

            in2 = new BufferedInputStream(new java.net.URL(urldisplay).openStream());
            mIcon11 = BitmapFactory.decodeStream(in2, null, options);


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

    public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

这是我得到的堆栈跟踪错误:

11-01 08:40:11.659   W/System.err﹕        java.io.IOException: unexpected end of stream
11-01 08:40:11.669  W/System.err﹕ at  libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:48)
11-01 08:40:11.669   W/System.err﹕ at java.io.InputStream.read(InputStream.java:163)
11-01 08:40:11.669  1 W/System.err﹕ at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
11-01 08:40:11.669  W/System.err﹕ at java.io.BufferedInputStream.read(BufferedInputStream.java:309)
11-01 08:40:11.669   W/System.err﹕ at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
11-01 08:40:11.679  W/System.err﹕ at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
11-01 08:40:11.679  W/System.err﹕ at com.halcyonsystems.nzbtrend.MovieInfo$DownloadImageTask.doInBackground(MovieInfo.java:226)
11-01 08:40:11.679  W/System.err﹕ at .MovieInfo$DownloadImageTask.doInBackground(MovieInfo.java:195)
11-01 08:40:11.679  W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-01 08:40:11.679  W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-01 08:40:11.689  W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-01 08:40:11.689  W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-01 08:40:11.689  W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-01 08:40:11.699  W/System.err﹕ at java.lang.Thread.run(Thread.java:856)

1 个答案:

答案 0 :(得分:0)

尝试关闭onPause()方法中从URL获得的流?