我想從「url」這是我的SD卡中包含大約40個圖像到我的閱讀器中的文件夾中的圖像,但我不斷收到內存不足的錯誤。無論如何解決它?可能是因爲我試圖一次加載40個圖像這就是爲什麼它導致錯誤?內存不足的一個字節分配,asynctask 5
下面是我的logcat輸出
02-01 15:38:17.410: D/url(10695): /mnt/sdcard/Android/data/com.sfa.coverflow/Issues/20/1944628064
02-01 15:38:17.440: D/dalvikvm(10695): GC_FOR_ALLOC freed 7144K, 32% free 16978K/24711K, paused 23ms
02-01 15:38:17.440: I/dalvikvm-heap(10695): Forcing collection of SoftReferences for 33033616-byte allocation
02-01 15:38:17.460: D/dalvikvm(10695): GC_BEFORE_OOM freed 9K, 32% free 16969K/24711K, paused 25ms
02-01 15:38:17.460: E/dalvikvm-heap(10695): Out of memory on a 33033616-byte allocation.
02-01 15:38:17.460: I/dalvikvm(10695): "AsyncTask #5" prio=5 tid=17 RUNNABLE
02-01 15:38:17.460: I/dalvikvm(10695): | group="main" sCount=0 dsCount=0 obj=0x411358f0 self=0x468d20
02-01 15:38:17.460: I/dalvikvm(10695): | sysTid=10718 nice=10 sched=0/0 cgrp=bg_non_interactive handle=3779488
02-01 15:38:17.460: I/dalvikvm(10695): | schedstat=(48743000 7345000 28) utm=4 stm=0 core=1
02-01 15:38:17.460: I/dalvikvm(10695): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-01 15:38:17.460: I/dalvikvm(10695): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
02-01 15:38:17.460: I/dalvikvm(10695): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
02-01 15:38:17.460: I/dalvikvm(10695): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:324)
02-01 15:38:17.460: I/dalvikvm(10695): at com.sfa.touchview.UrlTouchImageView$ImageLoadTask.doInBackground(UrlTouchImageView.java:108)
02-01 15:38:17.460: I/dalvikvm(10695): at com.sfa.touchview.UrlTouchImageView$ImageLoadTask.doInBackground(UrlTouchImageView.java:1)
02-01 15:38:17.460: I/dalvikvm(10695): at android.os.AsyncTask$2.call(AsyncTask.java:264)
02-01 15:38:17.460: I/dalvikvm(10695): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-01 15:38:17.460: I/dalvikvm(10695): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-01 15:38:17.460: I/dalvikvm(10695): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-01 15:38:17.460: I/dalvikvm(10695): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-01 15:38:17.460: I/dalvikvm(10695): at java.lang.Thread.run(Thread.java:856)
02-01 15:38:17.460: D/skia(10695): --- decoder->decode returned false
02-01 15:38:17.470: W/dalvikvm(10695): threadid=17: thread exiting with uncaught exception (group=0x40a4f1f8)
02-01 15:38:17.470: E/AndroidRuntime(10695): FATAL EXCEPTION: AsyncTask #5
02-01 15:38:17.470: E/AndroidRuntime(10695): java.lang.RuntimeException: An error occured while executing doInBackground()
02-01 15:38:17.470: E/AndroidRuntime(10695): at android.os.AsyncTask$3.done(AsyncTask.java:278)
02-01 15:38:17.470: E/AndroidRuntime(10695): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-01 15:38:17.470: E/AndroidRuntime(10695): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-01 15:38:17.470: E/AndroidRuntime(10695): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-01 15:38:17.470: E/AndroidRuntime(10695): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-01 15:38:17.470: E/AndroidRuntime(10695): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-01 15:38:17.470: E/AndroidRuntime(10695): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-01 15:38:17.470: E/AndroidRuntime(10695): at java.lang.Thread.run(Thread.java:856)
02-01 15:38:17.470: E/AndroidRuntime(10695): Caused by: java.lang.OutOfMemoryError
02-01 15:38:17.470: E/AndroidRuntime(10695): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-01 15:38:17.470: E/AndroidRuntime(10695): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
02-01 15:38:17.470: E/AndroidRuntime(10695): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
02-01 15:38:17.470: E/AndroidRuntime(10695): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:324)
02-01 15:38:17.470: E/AndroidRuntime(10695): at com.sfa.touchview.UrlTouchImageView$ImageLoadTask.doInBackground(UrlTouchImageView.java:108)
02-01 15:38:17.470: E/AndroidRuntime(10695): at com.sfa.touchview.UrlTouchImageView$ImageLoadTask.doInBackground(UrlTouchImageView.java:1)
02-01 15:38:17.470: E/AndroidRuntime(10695): at android.os.AsyncTask$2.call(AsyncTask.java:264)
02-01 15:38:17.470: E/AndroidRuntime(10695): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-01 15:38:17.470: E/AndroidRuntime(10695): ... 4 more
這是在錯誤發生
public class ImageLoadTask extends AsyncTask<String, Integer, Bitmap>
{
//RETRIEVES LINK FROM GALLERYACTIVITY
//READ MNT/SDCARD/....
@Override
protected Bitmap doInBackground(String... strings) {
bm = null;
String url = strings[0];
Log.d("url",url);
bm = BitmapFactory.decodeFile(url);
return bm;
}
嗨,我該如何實現代碼?對不起,我不是很擅長編程 – user1234555
嗨,我已經實現了你的代碼,並做了mImageView.setImageBitmap(getResizedBitmap(bm,700,500));但我仍然得到相同的錯誤:( – user1234555
不,你應該使用decodeSampledBitmapResource不getResizedBitmap如果你仍然有相同的問題嘗試減少位圖的大小,例如decodeSampledBitmapFromResource(getResource(),R.drawable.mImage,100,100) – bladeX