Memory Error Using Bitmap

At runtime, I try to put the image in a shallow view. When I tried to use the image from the "Drawable" folder, I got a "Out of memory" error. After a quick search on stackoverflow, I found that there would be some relief if we look at the image from the assets folder. But still I get an Out of memory error at runtime.

I analyzed and found that scaling will help in solving such memory problems. The fact is that I have an image size of 1280 x 720, and the size of the device is also the same. Therefore, I feel that scaling will have no effect.

Since we have experts in this community, I would appreciate if you can help me with some suggestions / examples to solve this problem.

Scenario 1:

Using the Bitmap from Drawable folder.

backgoundImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.backgroundhomepage), (int) dWidth, (int) dHeight, true); /*********************************************************************************************************************************************************** 1. To get the image from asset library **************************************************************************************************************************************************************/ public Bitmap getAssetImage(Context context, String filename) throws IOException { AssetManager assets = context.getResources().getAssets(); InputStream buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".png"))); Bitmap bitmap = BitmapFactory.decodeStream(buffer); return bitmap; } 

Scenario 2:

Using the Bitmap from Assets Folder

 backgoundImage = Bitmap.createScaledBitmap(getAssetImage(context,"backgroundhomepage"), (int) dWidth, (int) dHeight, true); 
+2
source share
3 answers

OutofMemory occurs when your application exceeds the memory allocated on the heap. The bitmap is too large to fit in memory, i.e. heaps. In this case, you do not have enough memory. You need to scale the bitmap and then use it. To do this, check the link below

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

There is also a blog @ http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html (avoiding memory leaks)

  public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){ try { //Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f),null,o); //The new size we want to scale to final int REQUIRED_WIDTH=WIDTH; final int REQUIRED_HIGHT=HIGHT; //Find the correct scale value. It should be the power of 2. int scale=1; while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT) scale*=2; //Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize=scale; return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); } catch (FileNotFoundException e) {} return null; } 

Quote from the docs

The BitmapFactory class provides several decoding methods (decodeByteArray (), decodeFile (), decodeResource (), etc.) for creating a bitmap from different sources. Choose the most suitable decoding method based on the image data source. These methods try to allocate memory for the constructed bitmap and therefore can easily lead to an OutOfMemory exception. Each type of decoding method has additional signatures that allow you to specify decoding options through the BitmapFactory.Options class.

Setting the inJustDecodeBounds property to true during decoding avoids memory allocation by returning null for the bitmap object, but setting outWidth, outHeight and outMimeType. This method allows you to read the size and type of image data before building (and allocating memory) a raster image.

Also check out this link for memory management.

https://www.youtube.com/watch?v=_CruQY55HOk

+3
source

Got a quick solution

 <application android:largeHeap="true" > 

placed in the appplication tag in the manifest file.

+2
source

You can use the following code to load a bitmap from a file:

 private Bitmap decodeFile(File f,int req_Height,int req_Width){ try { //decode image size BitmapFactory.Options o1 = new BitmapFactory.Options(); o1.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f),null,o1); //Find the correct scale value. It should be the power of 2. int width_tmp = o1.outWidth; int height_tmp = o1.outHeight; int scale = 1; if(width_tmp > req_Width || height_tmp > req_Height) { int heightRatio = Math.round((float) height_tmp / (float) req_Height); int widthRatio = Math.round((float) width_tmp / (float) req_Width); scale = heightRatio < widthRatio ? heightRatio : widthRatio; } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; o2.inScaled = false; return BitmapFactory.decodeFile(f.getAbsolutePath(),o2); } catch(Exception e) { e.printStackTrace(); } return null; } 

It should eliminate the exception from memory. The link here has a good detailed explanation of your answer.

0
source

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


All Articles