Skip to content

Instantly share code, notes, and snippets.

@chittaranjan-khuntia
Last active June 24, 2016 07:39
Show Gist options
  • Save chittaranjan-khuntia/0ba8478aa69557f400647735cf3ffcdc to your computer and use it in GitHub Desktop.
Save chittaranjan-khuntia/0ba8478aa69557f400647735cf3ffcdc to your computer and use it in GitHub Desktop.
Detect image orientation
private Bitmap bitmap = null;
private void gellerySelectedPhoto(Uri imageUri){
Uri uri = imageUri;
try{
String path =getRealPathFromURI(uri);
ExifInterface exif = new ExifInterface(path);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(angle);
Bitmap bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment