Is there a way to rotate an Image without loading into Memory in Android?

168 Views Asked by At

Is there a way to read the orientation and rotate if needed?

I want the user to take a picture by the camera. After that I check if the orientation is portrait. If not, rotate the Image and save it in internal storage for later use. (I have to store it in internal storage, for the case that the user deletes the image in gallery).

So here is my way to rotate the image:

int orientation = 0;
try {
    ExifInterface exif = new ExifInterface(mCurrentPhotoPath);
    orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
} catch (IOException e) {
    e.printStackTrace();
}

Matrix matrix = new Matrix();
matrix.postRotate(orientation);
Bitmap decodeFile = BitmapFactory.decodeFile(mCurrentPhotoPath);
Bitmap rotatedBitmap = Bitmap.createBitmap(decodeFile,0,0,decodeFile.getWidth(),decodeFile.getHeight(),matrix,true);
FileOutputStream out = null;

File pictureDir = new File(getFilesDir() + "/images/");
randomUUID = UUID.randomUUID().toString();
File file = new File(pictureDir.getAbsolutePath(), randomUUID + ".jpg");


try {
    imagePath = file.getAbsolutePath();
    out = new FileOutputStream(file);

    rotatedBitmap.compress(Bitmap.CompressFormat.JPEG,100,out);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (out != null) {
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

At line Bitmap decodeFile = BitmapFactory.decodeFile(mCurrentPhotoPath), I get on old devices an OOM. I tried to use BitmapFactory.Options and inJustDecodeBounds=true but the rotatedImage is null.

So how to just read the orientation and rotate the image while saving it in internal storage?

I've read many posts in stackoverflow but none of them had the same case.

Is it possible without loading it in memory?

Thank you for any help!

Kind Regards, raymondis

0

There are 0 best solutions below