Android image selected from gallery Orientation is always 0 : Exif TAG
android, bitmap, exif
Solution
After you get Uri of selected Image call the below functions using this methods, Like this,
Uri selectedImageUri = data.getData();
Bitmap bitmap = scaleImage(this,selectedImageUri);
The two functions to include in your activity are,
public static Bitmap scaleImage(Context context, Uri photoUri) throws IOException {
InputStream is = context.getContentResolver().openInputStream(photoUri);
BitmapFactory.Options dbo = new BitmapFactory.Options();
dbo.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, dbo);
is.close();
int rotatedWidth, rotatedHeight;
int orientation = getOrientation(context, photoUri);
if (orientation == 90 || orientation == 270) {
rotatedWidth = dbo.outHeight;
rotatedHeight = dbo.outWidth;
} else {
rotatedWidth = dbo.outWidth;
rotatedHeight = dbo.outHeight;
}
Bitmap srcBitmap;
is = context.getContentResolver().openInputStream(photoUri);
if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
float maxRatio = Math.max(widthRatio, heightRatio);
// Create the bitmap from file
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = (int) maxRatio;
srcBitmap = BitmapFactory.decodeStream(is, null, options);
} else {
srcBitmap = BitmapFactory.decodeStream(is);
}
is.close();
/*
* if the orientation is not 0 (or -1, which means we don't know), we
* have to do a rotation.
*/
if (orientation > 0) {
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
srcBitmap.getHeight(), matrix, true);
}
String type = context.getContentResolver().getType(photoUri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (type.equals("image/png")) {
srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
} else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
}
byte[] bMapArray = baos.toByteArray();
baos.close();
return BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
}
public static int getOrientation(Context context, Uri photoUri) {
/* it's on the external media. */
Cursor cursor = context.getContentResolver().query(photoUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
if (cursor.getCount() != 1) {
return -1;
}
cursor.moveToFirst();
return cursor.getInt(0);
}
To get Uri from File use this :
public static Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID },
MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
Q. Can you plz tell me what is "MAX_IMAGE_DIMENSION"? – Dhrupal Mar 13 '13 A. its based on your imageview size or the image size what you want to show.. whatever it is 60X60 then 60, 100X100 then 100 @Dhrupal
Problem
When I pick an image from gallery to set it on my imageview in android application then I see that it comes inverted by 180 or 270 or 90 degress. So to check/solve this I used `EXIF` orientations, but it always gives me "0". I am not able to get what is the problem. code: ``` Uri selectedImageUri = data.getData(); absolutePath = selectedImageUri.getPath(); exifMedia = new ExifInterface(absolutePath); String exifOrint = exifMedia.getAttribute(ExifInterface.TAG_ORIENTATION); exifOrientation = Integer.parseInt(exifOrint); System.out.println("Orientation Tag is:"+exifOrientation); /** Convert URI into byte */ ContentResolver cr = getBaseContext() .getContentResolver(); InputStream inputStream = cr .openInputStream(selectedImageUri); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); rotatedBMP = getResizedBitmapImage( bitmap, 100, 100,exifOrientation); ByteArrayOutputStream stream = new ByteArrayOutputStream(); rotatedBMP.compress(Bitmap.CompressFormat.PNG, 100, stream); byteArray = stream.toByteArray(); mimProfileImageBrowse.setImageBitmap(rotatedBMP); ``` method: ``` public Bitmap getResizedBitmapImage(Bitmap bm, int newHeight, int newWidth, int exifOrientation) { int width = bm.getWidth(); int height = bm.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; /** * create a matrix for the manipulation */ Matrix matrix = new Matrix(); /** * resize the bit map */ /* 1 = Horizontal (normal) 2 = Mirror horizontal 3 = Rotate 180 4 = Mirror vertical 5 = Mirror horizontal and rotate 270 CW 6 = Rotate 90 CW 7 = Mirror horizontal and rotate 90 CW 8 = Rotate 270 CW */ switch (exifOrientation) { case ExifInterface.ORIENTATION_ROTATE_270: rotate = 270; break; case ExifInterface.ORIENTATION_ROTATE_180: rotate = 180; break; case ExifInterface.ORIENTATION_ROTATE_90: rotate = 90; break; case ExifInterface.ORIENTATION_TRANSPOSE: rotate = 45; break; default: break; } matrix.postScale(scaleWidth, scaleHeight); matrix.postRotate(rotate); /** * recreate the new Bitmap */ Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); resizedBitmap = Bitmap.createScaledBitmap(bm, 65, 65, true); return resizedBitmap; } ``` Please help. Also `matrix.postROTATE` is not rotating the bitmaps. I dont know why. Thanks