Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save Exif data after bitmap coppression in Android

Tags:

android

bitmap

I need to get image from sd card, create, rotate and save changed image. I try to use this code

Bitmap original = BitmapFactory.decodeFile(file.getAbsolutePath());

    ExifInterface originalExif = new ExifInterface(file.getAbsolutePath());
    int orientation = originalExif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

    Matrix matrix = new Matrix();
    int rotate = 90;
    if(orientation == ExifInterface.ORIENTATION_ROTATE_90){
        rotate = 180;
    }else if(orientation == ExifInterface.ORIENTATION_ROTATE_180){
        rotate = 270;
    }else if(orientation == ExifInterface.ORIENTATION_ROTATE_270){
        rotate = 0;
    }

    matrix.postRotate(rotate);

    Bitmap bitmap = Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(), matrix, true);

    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        original.recycle();
        bitmap.recycle();
    }


    ExifInterface newExif = new ExifInterface(file.getAbsolutePath());

    newExif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_ROTATE_90));

    newExif.saveAttributes();

But i cant save change in ExifInterface. This just clear all the tags.

like image 915
couldDog Avatar asked Mar 18 '26 23:03

couldDog


1 Answers

saveAttributes method only Save the tag data into the JPEG file.

check this link

http://developer.android.com/reference/android/media/ExifInterface.html#saveAttributes()

So if you change your code this

bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

to this

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

it will save you exif tag data

Hope this help

Let me know in case of any other issue

like image 150
Sandy Avatar answered Mar 20 '26 13:03

Sandy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!