2013-02-21 149 views
0

我認爲我的錯誤日誌會清楚地描述我的問題。Android下載圖片隨機失敗

我在產品展示中使用Async做了很少的圖像下載。但是,當下載(可能較大)圖像的應用程序被迫關閉,注意:其他一些產品正確下載並顯示沒有問題。

下面是一些我在的AsyncTask代碼,我執行DownloadImage(URL)doInBackground並將其存儲在一個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) 
+0

我確定這句話'BufferedInputStream(in,32 * 4096);'導致出現問題 – 2013-02-21 07:07:48

+0

對不起,我只是使用了一些教程代碼,現在該怎麼辦?我試圖把更小或更大的數字,但它仍然不工作。 – 2013-02-21 07:09:54

回答

0

你要回收的位圖。位圖實現是本地的,因此java對象很小,並且是垃圾收集的候選對象,但內存仍然是分配的。看看Bitmap.recycle()

+0

我發現這個問題只發生在我的bitmapArray正在接收一些大尺寸圖像,任何想法? – 2013-02-22 04:37:44