我有人在我的應用程序中加載一堆圖像時出錯,我不確定是什麼原因導致它。獲取異常OutofMemoryError
這是他得到的錯誤。 LINE 135位於底部。
我在想他的手機內存不足。他正在使用2010年製作的Desire HD。該方法所做的是從服務器獲取圖像並將其保存在散列圖中。隨着顯示在屏幕上。關於如何防止這種OUTMEMORYERROR的任何想法?
我在想如果有超過20個圖像,設置一個計數器,只清除哈希映射。
的logcat:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
at com.codalata.craigslistchecker.GETTHEIMAGE$ITask.doInBackground(GETTHEIMAGE.java:135)
at com.codalata.craigslistchecker.GETTHEIMAGE$ITask.doInBackground(GETTHEIMAGE.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
代碼:
public class GETTHEIMAGE {
private HashMap<String, Drawable> ICache;
private static Drawable DefIcon = null;
private BaseAdapter adapt;
private ItemView item;
private int num = 0;
public GETTHEIMAGE(Context c) {
ICache = new HashMap<String, Drawable>();
}
public Drawable GetTheImagedata(
com.codalata.craigslistchecker.Favorites.TheListAdapter theListAdapter,
ImageView ICON_IMG) {
this.adapt = theListAdapter;
String url = (String) ICON_IMG.getTag();
if (ICache.containsKey(url)) {
return ICache.get(url);
} else {
new ITask().execute(url);
return DefIcon;
}
}
public Drawable GetTheImagedata(TheListAdapter adapter, ImageView ICON_IMG) {
this.adapt = adapter;
String url = (String) ICON_IMG.getTag();
if (ICache.containsKey(url)) {
return ICache.get(url);
} else {
new ITask().execute(url);
return DefIcon;
}
}
public Drawable GetTheImagedata2(ItemView itemView, String string) {
this.item = itemView;
String url = string;
if (ICache.containsKey(url)) {
return ICache.get(url);
} else {
new ITask2().execute(url);
return DefIcon;
}
}
private class ITask2 extends AsyncTask<String, Void, Drawable> {
private String Nick;
@Override
protected Drawable doInBackground(String... params) {
Nick = params[0];
InputStream isImage = null;
try {
URL url = new URL(Nick);
isImage = url.openStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return Drawable.createFromStream(isImage, "Nick");
}
@Override
protected void onPostExecute(Drawable result) {
super.onPostExecute(result);
if (num == 0) {
item.ivItemimage1.setImageDrawable(result);
}
if (num == 1) {
item.ivItemimage2.setImageDrawable(result);
}
if (num == 2) {
item.ivItemimage3.setImageDrawable(result);
}
if (num == 3) {
item.ivItemimage4.setImageDrawable(result);
}
if (num == 4) {
item.ivItemimage5.setImageDrawable(result);
}
if (num == 5) {
item.ivItemimage6.setImageDrawable(result);
}
if (num == 6) {
item.ivItemimage7.setImageDrawable(result);
}
if (num == 7) {
item.ivItemimage8.setImageDrawable(result);
}
if (num == 8) {
item.ivItemimage9.setImageDrawable(result);
}
if (num == 9) {
item.ivItemimage10.setImageDrawable(result);
}
num++;
}
}
private class ITask extends AsyncTask<String, Void, Drawable> {
private String Nick;
@Override
protected Drawable doInBackground(String... params) {
Nick = params[0];
InputStream isImage = null;
try {
URL url = new URL(Nick);
isImage = url.openStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try{
LINE 135 -----> return Drawable.createFromStream(isImage, "Nick");
}catch(Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Drawable result) {
super.onPostExecute(result);
synchronized (this) {
ICache.put(Nick, result);
}
adapt.notifyDataSetChanged();
}
}
}
20圖像的控制。每幅圖像的大小是多少?如果它太大,你需要縮小它們。此外它更好地緩存在SD卡/內部手機內存的圖像。 – Raghunandan
一些圖像很小,像200x200x和一些像1000x1000x大,問題是我需要立即擺脫它們。因爲這些傢伙手機很舊,無法處理緩存中的大量圖像。 – Xjasz