Android下载图片随机失败

时间:2013-02-21 07:04:40

标签: android bitmap android-asynctask

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

我在使用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;
}

log.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)

2 个答案:

答案 0 :(得分:1)

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

  1. Lazy list by Fedor
  2. Universal Image Loader
  3. ImageLoader by Novoda

答案 1 :(得分:0)

你必须回收位图。位图实现是本机的,因此java对象很小并且是垃圾收集的不良候选者,但仍然分配了内存。看看Bitmap.recycle()