我在下載圖像的過程中遇到後退按鈕(關閉活動)時出現問題。我有一個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)
爲什麼使用BufferedInputStream? – njzk2
嘗試重置BufferedInputStream對象 – Tejas
重置會執行什麼操作?我暫停/銷燬活動,所以我想關閉輸入流。我確實在onPause和onDestroy中有close方法,但它仍然拋出錯誤 – AutomationEngineer1982