I capture image from my cam and create bitmap and pass the path to ExifInterface
to determine the rotation, since the cam captured image is rotated 90 degrees all the time. But this below code always shows value 0
which corresponds to ORIENTATION_UNDEFINED
rotation type.
It is any other way to determine rotation or I am doing something wrong here ?
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = Utils.createImageFile(getContext());
String authorities = context.getPackageName() + ".fileprovider";
uriForFile = FileProvider.getUriForFile(context, authorities, photoFile);
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "handleCaptureImage: " + uriForFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
startActivityForResult(intent, CAPTURE_IMAGE);
if (requestCode == CAPTURE_IMAGE) {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uriForFile);
saveBitmap(bitmap);
}
public static File saveBitmap(Bitmap bitmap) {
File file = null;
String imageFileName = "JPEG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";
String path = App.getAppContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + imageFileName;
if (bitmap != null) {
file = new File(path);
try {
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(path);
//this bitmap is rotated when I observe in debug mode.
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
ExifInterface exifInterface = new ExifInterface(path);
//still always returns 0 I expect 6 which is for 90 degrees
Log.d(TAG, "saveBitmap: "+exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
}