在我的應用我從網上使用圖像的URL下載圖像。我有很多圖像所以我實現了這個分頁技術,我顯示了每個頁面的垂直順序15個圖像。在這種情況下,我正在向上/向下滾動頁面以查看圖像,此時我的應用程序崩潰了,並且出現了內存異常。請任何人都可以幫助我。如何避免內存不足的異常
的logcat:
02-07 11:23:52.256: ERROR/ACRA(7236): El Gifto fatal error : bitmap size exceeds VM budget(Heap Size=7943KB, Allocated=3485KB, Bitmap Size=12546KB)
02-07 11:23:52.256: ERROR/ACRA(7236): java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7943KB, Allocated=3485KB, Bitmap Size=12546KB)
02-07 11:23:52.256: ERROR/ACRA(7236): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-07 11:23:52.256: ERROR/ACRA(7236): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:690)
02-07 11:23:52.256: ERROR/ACRA(7236): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:490)
02-07 11:23:52.256: ERROR/ACRA(7236): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-07 11:23:52.256: ERROR/ACRA(7236): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
02-07 11:23:52.256: ERROR/ACRA(7236): at com.ibkr.elgifto.GiftCategories$itemlistadapter$3.getDrawableFromUrl(GiftCategories.java:837)
02-07 11:23:52.256: ERROR/ACRA(7236): at com.ibkr.elgifto.GiftCategories$itemlistadapter$3.run(GiftCategories.java:724)
這裏是我的代碼:
public void DownLoadImageInAThreadHandler(final CategoryData Item, final ViewHolder holder)
{
final Handler handler = new Handler()
{
@Override public void handleMessage(Message message)
{
holder.imgitem.setImageDrawable((Drawable) message.obj);
holder.imgitem.setVisibility(View.VISIBLE);
holder.progress.setVisibility(View.GONE);
}
};
//Thread for getting the attributes values
Thread t = new Thread()
{
public void run()
{
try
{
drawable = getDrawableFromUrl(Item.ImageUrl);
if(drawable != null)
{
//Send the message to the handler
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);
}
else
{
int idNoImage = R.drawable.giftsuggestionsnoimage;
Drawable dwgNoImg = GiftCategories.this.getResources().getDrawable(idNoImage);
//Send the message to the handler
Message message = handler.obtainMessage(1, dwgNoImg);
handler.sendMessage(message);
}
}
catch(Exception exp)
{
System.out.println("Exception in DownLoadImageInAThread : " + exp.getMessage());
}
}
private Drawable getDrawableFromUrl(String imageUrl) throws IOException
{
Drawable image = null;
try
{
InputStream in = (java.io.InputStream) new java.net.URL(imageUrl).getContent();
if (in != null)
{
image = Drawable.createFromStream(in, "image");
in.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
return image;
}
};
t.start();
}
這是發生在仿真器或真實設備? – 2012-02-07 06:25:02
在這兩種情況下(設備和仿真器)。 – naresh 2012-02-07 06:27:21
不知道確切的原因。我已經通過給system.gc()解決了這個問題。 。但是,這並不確切方式 – 2012-02-07 06:32:20