How to save Exif data after bitmap coppression in Android

怎甘沉沦 提交于 2020-01-01 09:22:37

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/33753634/how-to-save-exif-data-after-bitmap-coppression-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!