0

我认为我的错误日志会清楚地描述我的问题。

我正在使用 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)
4

2 回答 2

1

如果您正在下载多个图像,那么您应该使用如下所示的一些缓存算法,这样您就可以缓存图像并轻松地从缓存中加载它。

  1. Fedor 的懒惰列表
  2. 通用图像加载器
  3. 诺沃达的 ImageLoader
于 2013-02-21T07:10:47.667 回答
0

you have to recycle bitmap. The bitmap implementation is native, so the java object is small and a poor candidate for java garbage collection but the memory is still allocated. Take a look to Bitmap.recycle()

于 2013-02-21T07:15:03.440 回答