我认为我的错误日志会清楚地描述我的问题。
我正在使用 Async 在产品展示中进行少量图像下载。但是当下载(可能更大)图像时应用程序强制关闭,注意:其他一些产品正确下载并显示没有问题。
这是我在 asynctask 中的一些代码,我在 doInBackground 中执行了DownloadImage(URL)并将其存储在bitmapArray
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
BufferedInputStream bis = new BufferedInputStream(in, 32 * 4096);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
byte[] imageData = baf.toByteArray();
bitmap = BitmapFactory.decodeByteArray(imageData,0,imageData.length);
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
日志.txt
02-21 06:54:35.982: E/AndroidRuntime(2026): FATAL EXCEPTION: AsyncTask #3
02-21 06:54:35.982: E/AndroidRuntime(2026): java.lang.RuntimeException: An error occured while executing doInBackground()
02-21 06:54:35.982: E/AndroidRuntime(2026): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-21 06:54:35.982: E/AndroidRuntime(2026): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-21 06:54:35.982: E/AndroidRuntime(2026): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-21 06:54:35.982: E/AndroidRuntime(2026): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-21 06:54:35.982: E/AndroidRuntime(2026): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-21 06:54:35.982: E/AndroidRuntime(2026): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-21 06:54:35.982: E/AndroidRuntime(2026): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-21 06:54:35.982: E/AndroidRuntime(2026): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-21 06:54:35.982: E/AndroidRuntime(2026): at java.lang.Thread.run(Thread.java:856)
02-21 06:54:35.982: E/AndroidRuntime(2026): Caused by: java.lang.OutOfMemoryError
02-21 06:54:35.982: E/AndroidRuntime(2026): at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
02-21 06:54:35.982: E/AndroidRuntime(2026): at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:426)
02-21 06:54:35.982: E/AndroidRuntime(2026): at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:444)
02-21 06:54:35.982: E/AndroidRuntime(2026): at com.nazartt.angtrading.ShowCase$task.DownloadImage(ShowCase.java:201)
02-21 06:54:35.982: E/AndroidRuntime(2026): at com.nazartt.angtrading.ShowCase$task.doInBackground(ShowCase.java:129)
02-21 06:54:35.982: E/AndroidRuntime(2026): at com.nazartt.angtrading.ShowCase$task.doInBackground(ShowCase.java:1)
02-21 06:54:35.982: E/AndroidRuntime(2026): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-21 06:54:35.982: E/AndroidRuntime(2026): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)