How to save Exif data after bitmap compression in Android

I need to get the image from the SD card, create, rotate and save the modified image. I am trying 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 can not save the changes to ExifInterface. This will simply clear all tags.

+5
source share
1 answer

saveAttributes method Save tag data to a JPEG file.

check this link

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

So, if you change the code of this

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

to that

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

it will save your exif tag data

Hope for this help

Let me know in case of any other problem.

+2
source

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


All Articles