Reputation: 479
setRotation method
in Camera.Parameters
does not work in all devices. Somebody suggests to manually change the EXIF
information to solve the problem. Can you give me a short example on how to set the exif
information with ExifInterface
in such a way to set the image orientation as portrait?
private int savePicture(byte[] data)
{
File pictureFile = getOutputMediaFile();
if (pictureFile == null)
return FILE_CREATION_ERROR;
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
return FILE_NOT_FOUND;
} catch (IOException e) {
return ACCESSING_FILE_ERROR;
}
return OKAY;
}
I've tried with this:
try {
ExifInterface exifi = new ExifInterface(pictureFile.getAbsolutePath());
exifi.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_ROTATE_90));
exifi.saveAttributes();
} catch (IOException e) {
Log.e(TAG, "Exif error");
}
but nothing change when I visualize the pictures from the android gallery.
Upvotes: 7
Views: 25423
Reputation: 11
its below code is work for me ..
File imageFile = new File(string_path); //add path here
ExifInterface exif = null;
try {
exif = new ExifInterface(imageFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch (orientation) {
case ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
this.btmap = Bitmap.createBitmap(this.btmap, 0, 0, this.btmap.getWidth(), this.btmap.getHeight(), matrix, true);
Glide.with(this).load(this.btmap).into((ImageView) getActivity().findViewById(R.id.btnimg));
Upvotes: 1
Reputation: 6050
For those who actually want to write these EXIF information out, here is some code:
ExifInterface exifInterface = new ExifInterface(someFile.getPath());
exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION,
String.valueOf(orientation));
exifInterface.saveAttributes();
whereas orientation
is one of the standard orientations, i.e. ExifInterface.ORIENTATION_ROTATE_{90,180,270}
.
Upvotes: 11
Reputation: 3473
If orientation is saved into file but doesn't appear in the gallery it may be because of orientation is cached in MediaStore. So you need to try to update this information there also.
Here is the code snipped (untested)
/**
* @param fileUri the media store file uri
* @param orientation in degrees 0, 90, 180, 270
* @param context
* @return
*/
public boolean setOrientation(Uri fileUri, int orientation, Context context) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.ORIENTATION, orientation);
int rowsUpdated = context.getContentResolver().update(fileUri, values, null, null);
return rowsUpdated > 0;
}
/**
* Get content uri for the file path
*
* @param path
* @param context
* @return
*/
public Uri getContentUriForFilePath(String path, Context context) {
String[] projection = {
MediaStore.Images.Media._ID
};
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
MediaStore.Images.Media.DATA + " = ?", new String[] {
path
}, null);
Uri result = null;
if (cursor != null) {
try {
if (cursor.moveToNext()) {
long mediaId = cursor.getLong(0);
result = ContentUris.withAppendedId(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mediaId);
}
} finally {
cursor.close();
}
}
return result;
}
Upvotes: 3
Reputation: 1194
Well, I was stuck in such kind of problem, and tried the solution that you are working on and ended up using Matrix function.
Whatever the images I was taking, it was all landscape, so in app I just applied the below code, and it worked well:-
Matrix matrix = new Matrix();
//set image rotation value to 90 degrees in matrix.
matrix.postRotate(90);
//supply the original width and height, if you don't want to change the height and width of //bitmap.
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, newWidth, newHeight, matrix, true);
Upvotes: 1