Bit memory card error in android ...

I work in an Android application and I use a bitmap to snap the image to an ImageView. My requirement is to rotate this ImageView and provide a border to this ImageView. I have successfully implemented this, but after the application uses this activity two to three times, the message "force close" appears, talking about the Bitmap from the VM memory. Please help me minimize the memory consumption of the bitmap in my code. And let me know how to change the code for the same?

final int BORDER_WIDTH = 5; // Set the border color final int BORDER_COLOR = Color.WHITE; Bitmap res = Bitmap.createBitmap(CAPTURE_IMAGE.getWidth() + 2 * BORDER_WIDTH, CAPTURE_IMAGE.getHeight() + 2 * BORDER_WIDTH, CAPTURE_IMAGE.getConfig()); System.gc(); Canvas canvas = new Canvas(res); Paint paint = new Paint(); paint.setColor(BORDER_COLOR); canvas.drawRect(0, 0, res.getWidth(), res.getHeight(), paint); canvas.drawBitmap(CAPTURE_IMAGE, BORDER_WIDTH, BORDER_WIDTH, paint); Matrix mat = new Matrix(); // Set the Imageview position mat.postRotate(355); bMapRotate = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), mat, true); System.gc(); res.recycle(); res = null; paint = null; canvas = null; mat = null; // Set the captured bitmap image in the imageview mShareImageView.setImageBitmap(bMapRotate); 
+6
source share
4 answers

I think you should use a shrink function like this

 bMapRotate = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), mat, true); Bitmap myBitmap = ShrinkBitmap(bMapRotate , 300, 300); mShareImageView.setImageBitmap(myBitmap ); private Bitmap ShrinkBitmap(String file, int width, int height) { // TODO Auto-generated method stub BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); bmpFactoryOptions.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height); int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width); if (heightRatio > 1 || widthRatio > 1) { if (heightRatio > widthRatio) { bmpFactoryOptions.inSampleSize = heightRatio; } else { bmpFactoryOptions.inSampleSize = widthRatio; } } bmpFactoryOptions.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); return bitmap; } 

it worked for me and I escaped bitmap from VM memory exception

+3
source

in the mainfest file add ---> android: largeHeap: "true"

+3
source

Try moving the gc() call to the end. It should start after setting res = null so that it can free up unused memory:

  res.recycle(); res = null; paint = null; canvas = null; mat = null; System.gc(); 
+1
source

Just add android: largeHeap = "true" in the manifest file

Ref: largeHeap = true manifest doesn't work?

0
source

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


All Articles