简体   繁体   中英

java.io.IOException: unexpected end of stream (Android)

I'm having an issue when hitting the back button (closing an activity) while it is in the process of downloading an image. I have an asynctask setup to download the image (using the process defined in Android's developer site) yet I'm encountering a crash with an unexpected end of stream if the image hasn't fully downloaded yet and I hit the back button to return to the previous activity.

Here's my asynctask and the method which resizes the image (if it's too large):

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

Here's the stacktrace error I'm getting:

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM