簡體   English   中英

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

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

在下載圖像的過程中按下返回按鈕(關閉活動)時出現問題。 我有一個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;
}

這是我得到的stacktrace錯誤:

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獲得的流嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM