How to work with a large raster image. Rotate and insert into gallery

I need to take a picture with the camera and, if depending on the size of the image, rotate it before saving it to the gallery.

I use

Intent imageCaptureIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE); imageCaptureIntent.putExtra (MediaStore.EXTRA_OUTPUT, uri); startActivityForResult (imageCaptureIntent, IMAGE_CAPTURE);

Take a picture and save it to a temporary file.

Then

Bitmap bmp = BitmapFactory.decodeFile (imagePath);
   String str = android.provider.MediaStore.Images.Media.insertImage (cr, bmp, name, description);

To save it.

This is the code I tried to use to rotate a bitmap

= ();    matrix.postRotate(180);
    x = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);    android.provider.MediaStore.Images.Media.insertImage(cr, x, , );

, OutOfMemoryException.

, ?

~ ,

+5
2

, . [] , ; , , , .

:

  • android.provider.MediaStore.Images.Media.insertImage(cr, imagePath, name, description) android.provider.MediaStore.Images.Media.insertImage(cr, bmp, name, description) , Bitmap bmp = BitmapFactory.decodeFile(imagePath), .

  • , , . , null , bmp.recycle().

+2

. :

    Bitmap bmp = BitmapFactory.decodeFile(imagePath); //this is the image you want to rotate
    // keeping in mind that you want to rotate the whole original image instead
    // of its downscaled copy you cant use BitmapFactory downscaling ratio
    Matrix matrix = new Matrix();
    matrix.postRotate(180);
    Bitmap x = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 
    // the line above creates another bitmap so we have here 2 same sized bitmaps
    // even using the same var (bmp instead of x) wont change anything here
    // so you gonna get the OOM here

, 2 , , x2 .
. ImageMagick lib.

0

All Articles