2013-11-01 65 views
0

我在下载图像的过程中遇到后退按钮(关闭活动)时出现问题。我有一个asynctask设置下载图像(使用Android开发人员网站中定义的过程),但如果图像尚未完全下载,我遇到意外流结束的崩溃,并且我点击后退按钮返回之前的活动。java.io.IOException:意外流结束(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) 
+0

为什么使用BufferedInputStream? – njzk2

+0

尝试重置BufferedInputStream对象 – Tejas

+0

重置会执行什么操作?我暂停/销毁活动,所以我想关闭输入流。我确实在onPause和onDestroy中有close方法,但它仍然抛出错误 – AutomationEngineer1982

回答

0

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

+0

我在我的onPause()方法中使用了这个方法:try(012)! if(in2!= null) in2.close(); catch(IOException e){ e.printStackTrace(); }并且崩溃时会出现相同的堆栈跟踪错误 – AutomationEngineer1982