How to avoid an exception from memory

In my application, I download images from the Internet using the image URL. I have a lot of images. So I implemented the swap technique for this, and I displayed 15 images for each page in vertical order. In this case, I scroll up / down the page to view images, at this time my application crashed and I selected an exception from memory. please can someone help me.

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) 

Here is my code:

 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(); } 
+4
source share
3 answers

If you have memory problems, you need to go with two options first. => You must decode the image using a bitmap or Second => Download the image and save it in a temporary folder and use it. When you do not need to delete all files from a folder.

Upload image and save it in a folder check this link

For decoding fooobar.com/questions/93742 / .... Maybe this will help you. Thanks

0
source

On ICS preliminary phones, bitmaps were created on the Dalwick heap, and their memory clearing was called in the finalizer, which is not a silver blaster http://code.google.com/p/android/issues/detail?id=8488

Use a raster object and make sure you use the recycle method

0
source

This is a common problem, and I think you should use the SoftReference collection for your bitmap. Check out documents and online resources for SoftReference: this will allow Android to clean up the space occupied by the bitmap when necessary.

0
source

Source: https://habr.com/ru/post/1395159/


All Articles