Processing a large bitmap in android

I just want to open my own application for the camera from my application, to take a picture and set it as the background image of my screen, and then turn it on the click button. A photo rotates n times if it is taken with a 3 megapixel camera. If I set the camera resolution to 5 or more points, the application will be forced to close when the button is pressed 5 times (the photo rotates 4 times).

Bitmap rotatedBitmap = null; int curAngle = 0; private Bitmap rotateImageBitmap(Bitmap capturedPhotoBitmap) { if(rotatedBitmap != null ) { rotatedBitmap = null; } Matrix matrix = new Matrix(); curAngle = (curAngle + 90) % 360; matrix.postRotate(curAngle); rotatedBitmap = Bitmap.createBitmap(capturedPhotoBitmap, 0, 0, capturedPhotoBitmap.getWidth(), capturedPhotoBitmap.getHeight(), matrix, true); return rotatedBitmap; } 

This is from the developer's guide ....
1. Mobile devices usually have limited system resources. Android devices can have only 16 MB of memory for one application.

2. Raster images take up a lot of memory, especially for rich images such as photographs. For example, a camera on a Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels) in size. If the raster configuration ARGB_8888 is used (by default, it is further from Android 2.3), then loading this image into memory takes about 19 MB of memory (2592 * 1936 * 4 bytes), immediately having exhausted the restriction for each application on some devices.

Most phones currently have 8 megapixels or larger cameras. so the pictures will be large. How can I rotate my photo ā€œnā€ several times without worrying about camera resolution. Do I need to squeeze it? What is the best way? Please help me.

+2
source share
2 answers

Instead of having the image on the heap in RAM, I uploaded (read: buffer) the image to disk in the directory of your application cache. http://developer.android.com/reference/android/content/Context.html#getCacheDir ()

After the image is on disk, I would then inflate it into memory using the sample size to reduce memory consumption.

See http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize and http://developer.android.com/reference/android/graphics/BitmapFactory.html

The size of the selection you select depends on the size of the bitmap (in pixels) and the size of the view that you load in the image (in pixels).

To find the image size, use the "inJustDecodeBounds" parameter in the BitmapFactory.Options object.

Remember that the sample size you choose must have a capacity of 2. The higher the number, the more memory you save, but the lower the image quality.

Sample size 2 = 1/4 of the size. Sample size 4 = 1/16 size, etc.

0
source

The Android Developers website has a whole tutorial on reading and displaying large bitmap images:

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

0
source

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


All Articles