How to avoid memory errors when using bitmaps in Android

I use bitmap images. When you run the code, a memory error is displayed. How to avoid a mistake. My code follows. Thanks in advance.

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); img_cook[index].setImageBitmap(myBitmap); public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth, int reqHeight) { Bitmap bm = null; final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(path, options); return bm; } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float)height / (float)reqHeight); } else { inSampleSize = Math.round((float)width / (float)reqWidth); } } 
+7
android
source share
4 answers

When you are done with your bitmap, it means that when your Bitmap has completed its work, make it recyle and null, as shown below:

 bitmap.recycle(); bitmap=null; 

OR

I think you are loading Image from url, so I suggest you use Android Query for this, you will never get this error if you use it.

You can download the jar file from here: http://code.google.com/p/android-query/downloads/list Download the jar file and install jar in your build path.

  AQuery androidAQuery=new AQuery(this); 

As an example, to download an image directly from a URL:

 androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW); 

As an example, to get a bitmap with a URL:

 androidAQuery.ajax(YOUR IMAGE URL,Bitmap.class,0,new AjaxCallback<Bitmap>(){ @Override public void callback(String url, Bitmap object, AjaxStatus status) { super.callback(url, object, status); //You will get Bitmap from object. } }); 

It is very fast and accurate, and with this you can find many other functions, such as animation at boot; obtaining a raster image, if necessary; and etc.

+8
source share

Now your image size is large, so use a width and height similar to this, and after setting the image clear chache

 Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 60, 60); img_cook[index].setImageBitmap(myBitmap); if (myBitmap != null) { bitmap.recycle(); bitmap = null; System.gc(); } 
+1
source share

I assume that you will not get an OOM exception after creating the first bitmap, but rather, does this happen after loading several bitmaps into memory?

Try to increase efficiency by manually processing () on bitmaps that you no longer need. While the GC collects some bitmap data that frees all its references, the actual image memory is stored in its own memory, so to free this memory you need to call the .recycle () bitmap when you need to free it.

Hope this helps.

0
source share

Android applications have a very low amount of memory. You must manage it carefully to avoid memory deletion. You can see google solution

0
source share

All Articles