hacker
hacker

Reputation: 8947

how to apply EXIF orientation for images as urls in android

I am dealing with camera images in my app which actually get as urls.The camera app is in portrait mode.So the images are sometimes left rotated or right rotated.I know that we can use EXIF for normal image orientation from gallery, there we can check the exif value and do appropriate changes.Basicaly this is the code for exif

ExifInterface exif = new ExifInterface("filepath");
exif.getAttribute(ExifInterface.TAG_ORIENTATION);

So my question is what is the filepath in my case, is it the url itself or should i create a file for that.. Any help will be greatly appreciated ....

Upvotes: 5

Views: 1576

Answers (1)

Evgen
Evgen

Reputation: 610

First, you have to get Uri in onActiviryResult, like here Get image Uri in onActivityResult after taking photo?
Then, when you have Uri you can get filepath like this: String filepath = getRealPathFromURI(someUri);

public String getRealPathFromURI(Uri contentUri) {
    String res = null;
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
    if(cursor != null && cursor.moveToFirst()){;
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}


Then, if filepath is not null and not empty, you can get EXIF data, check it and rotate image if it needs.
Hope it will help.

Upvotes: 1

Related Questions