i've been struggling with the problem below, i found some tutorials and made a custom camera with a SurfaceView as a preview holder.
I'm mainly concerned for the front Camera.
Now when i take an image while the preview works fine the image is being saved with a wrong rotation in my SD card.
Below are the main two functions. Setup of the camera and the take picture.
*private void setUpCamera(Camera c) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
rotation = getWindowManager().getDefaultDisplay().getRotation();
int degree = 0;
switch (rotation) {
case Surface.ROTATION_0:
degree = 0;
break;
case Surface.ROTATION_90:
degree = 90;
break;
case Surface.ROTATION_180:
degree = 180;
break;
case Surface.ROTATION_270:
degree = 270;
break;
default:
break;
}
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
// frontFacing
rotation = (info.orientation + degree) % 360;
rotation = (360 - rotation) % 360;
} else {
// Back-facing
rotation = (info.orientation - degree + 360) % 360;
}
c.setDisplayOrientation(rotation);
Parameters params = c.getParameters();
List<String> focusModes = params.getSupportedFlashModes();
if (focusModes != null) {
if (focusModes
.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
params.setFlashMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
}
}
params.setRotation(rotation);
c.setParameters(params);
}*
private void takeImage() {
camera.takePicture(null, null, new PictureCallback() {
private File imageFile;
#Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
// convert byte array into bitmap
Bitmap loadedImage = null;
loadedImage = BitmapFactory.decodeByteArray(data, 0,data.length);
// rotate Image
Matrix rotateMatrix = new Matrix();
rotateMatrix.postRotate(rotation);
loadedImage = Bitmap.createBitmap(loadedImage, 0, 0,loadedImage.getWidth(), loadedImage.getHeight(),rotateMatrix, false);
//create folder if it doesnt exists
String state = Environment.getExternalStorageState();
File folder = null;
if (state.contains(Environment.MEDIA_MOUNTED)) {
folder = new File(Environment
.getExternalStorageDirectory() + "/Squote");
} else {
folder = new File(Environment
.getExternalStorageDirectory() + "/Squote");
}
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
imageFile = new File(folder.getAbsolutePath()
+ File.separator
+ "IMG_"
+ timeStamp
+ ".jpg");
imageFile.createNewFile();
} else {
Toast.makeText(getBaseContext(), "Image Not saved",Toast.LENGTH_SHORT).show();
return;
}
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
// save image into gallery
loadedImage.compress(CompressFormat.JPEG, 100, ostream);
FileOutputStream fout = new FileOutputStream(imageFile);
fout.write(ostream.toByteArray());
fout.close();
ContentValues values = new ContentValues();
values.put(Images.Media.DATE_TAKEN,System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, imageFile.getAbsolutePath());
//release camera and return image
releaseCamera();
Intent intentMessage = new Intent();
intentMessage.putExtra("imageUri", imageFile.getAbsolutePath());
setResult(RESULT_OK, intentMessage);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
On the rotate image part of the code if i put 270 it shows up correct BUT i'm having another issue there. The image is not the same as the preview but it's mirrored. I think also that a fixed 270 value is not a solution as it messes up the back camera or the portrait pictures.
Any help would be much appreciated.
Thank you
It's not an issue, it's how the front-facing camera works. Even if you take a picture with your phone's camera app it will be flipped when saved. You can implement a method to flip the image vertically if that is the result you are trying to get.
Related
I'm creating android camera app. This is an custom camera using camera API.
Whenever I take picture from camera it always save in landscap mode.
I'm using this method to set the camera orientation.
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
Here is code which is responsible to store image.
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d(TAG, "Error creating media file, check storage permissions: ");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
This code save the image successfully but always in landscape mode. What I'm missing I'm new in android. I want to save image as like it is shown in preview.
Update 2 (According To Answer)
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Camera.CameraInfo info = new Camera.CameraInfo();
int rotationAngle = getCorrectCameraOrientation (MainActivity.this, info);
Toast.makeText(MainActivity.this, "Angle "+ rotationAngle, Toast.LENGTH_SHORT).show();
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
try {
writeFile (data, pictureFile);
} catch (IOException e) {
e.printStackTrace();
}
processImage (pictureFile, rotationAngle, 1);
}
};
I'm using code in this way but this is not working still same problem image is always in landscape mode.
Try
1. First of all, add methods (writeFile, processImage and getCorrectCameraOrientation) defined below (After step 3)
2. Calculate camera orientation after capturing photo and update onPictureTaken**
#Override
public void onPictureTaken (final byte[] data, final Camera camera) {
int rotationAngle = getCorrectCameraOrientation (this, info);
3. Create file and update photo angle using rotationAngle
File file = new File (folder, fileName);
try {
file.createNewFile ();
}
catch (IOException e) {
e.printStackTrace ();
}
writeFile (data, file);
processImage (file, rotationAngle, compressRatio);
writeFile
public static void writeFile (byte[] data, File file) throws IOException {
BufferedOutputStream bos = null;
try {
FileOutputStream fos = new FileOutputStream (file);
bos = new BufferedOutputStream (fos);
bos.write (data);
}
finally {
if (bos != null) {
try {
bos.flush ();
bos.close ();
}
catch (Exception e) {
}
}
}
}
processImage
public static void processImage (File file, int rotationAngle, int compressionRatio) {
BufferedOutputStream bos = null;
try {
Bitmap bmp = BitmapFactory.decodeFile (file.getPath ());
Matrix matrix = new Matrix ();
matrix.postRotate (rotationAngle);
bmp = Bitmap.createBitmap (bmp, 0, 0, bmp.getWidth (), bmp.getHeight (), matrix, true);
FileOutputStream fos = new FileOutputStream (file);
bmp.compress (Bitmap.CompressFormat.PNG, compressionRatio, fos);
}
catch (IOException e) {
e.printStackTrace ();
}
catch (OutOfMemoryError t) {
t.printStackTrace ();
}
catch (Throwable t) {
t.printStackTrace ();
}
finally {
if (bos != null) {
try {
bos.flush ();
bos.close ();
}
catch (Exception e) {
}
}
}
}
getCorrectCameraOrientation
public static int getCorrectCameraOrientation (Activity activity, Camera.CameraInfo info) {
int rotation = activity.getWindowManager ().getDefaultDisplay ().getRotation ();
int degrees = 0;
if (hasValidRotation (rotation)) {
degrees = rotation * 90;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360;
}
else {
result = (info.orientation - degrees + 360) % 360;
}
return result;
}
I have an activity to open camera intent and grab the photo in the onActivityResult method.
Three devices worked well without errors but only in one device I get null error on the file variable.
"Attempt to invoke virtual method java.lang.String java.io.File.getPath() on a null object reference"
This is the way how I implemented it.
A) Step one: Open camera
Upon clicking on a button I run the following code. This code will:
Create a new file
Open camera intent
//create file
imgName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";
//global variable
file = new File(Environment.getExternalStorageDirectory(), imgName);
if(file != null){
//save image here
Uri relativePath = Uri.fromFile(file);
//Open camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, relativePath);
startActivityForResult(intent, Constant.CAMERA_PIC_REQUEST);
}
B) Step two: Take a picture and click save button
C) Step three: Grab the picture in onActivityResult() method
Check the constant = Constant.CAMERA_PIC_REQUEST.
Get the EXIF data from the photo and find out the required angle to rotate the photo.
Set the width, height and angle. Create the .jpg photo
Save the created image name and path into imageArray
Set the grid view with imageArray
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case Constant.CAMERA_PIC_REQUEST:
try {
//Get EXIF data and rotate photo (1. file.getPath() has been used)
ExifInterface exif = new ExifInterface(file.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;// Landscape
//rotate photo
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 matrix = new Matrix();
matrix.postRotate(angle);
try {
//Create the rotated .jpg file (2. file.getPath() has been used)
Bitmap correctBmp = Func.decodeSampleBitmapFromFile(file.getPath(), Constant.PHOTO_WIDTH, Constant.PHOTO_HEIGHT);
correctBmp = Bitmap.createBitmap(correctBmp, 0, 0, correctBmp.getWidth(), correctBmp.getHeight(), matrix, true);
FileOutputStream fOut;
fOut = new FileOutputStream(file);
correctBmp.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
fOut.flush();
fOut.close();
//image saved successfully - add photo name into reference array. Later I will use this
//(3. file.getPath() has been used)
Image img = new Image();
img.setImageName(imgName);
img.setAbsolutePath(file.getAbsolutePath());
img.setRelativePath(Uri.fromFile(file));
imageArray.add(img);//save in array
} catch (FileNotFoundException e) {
Log.e("Error", e.getMessage());
} catch (IOException e) {
Log.e("Error", e.getMessage());
}
} catch (Exception e) {
//This exception is triggering.
Log.e("Error 15", e.getMessage());
}
//set this photo in a grid view for user to see
setGridView(imageArray);
break;
case Constant.POPUPFRAG:
//something else
break;
default:
//deafult
break;
}
}
}
Error 15 (third exception) is triggering only in one phone. I'm not sure why is that. It shows the null pointer exception that I posted above.
Three devices that could run above code is having at least android v4.0 or above.
The phone that gives me null exception is Samsung Galaxy Note 3 Android v5.0. (Although the code is failing, I could find the photo in the phone gallery)
Can you please suggest me what could go wrong here? Am I missing something?
Anyway to improve this code?
Thanks!
Instead of directly using file.getPath() you can get use like this.
Create Global varibale of Uri then use it in all activity.
private Uri imageToUploadUri;
file = new File(Environment.getExternalStorageDirectory(), imgName);
if(file != null){
//save image here
imageToUploadUri = Uri.fromFile(file);
//Open camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageToUploadUri);
startActivityForResult(intent, Constant.CAMERA_PIC_REQUEST);
}
then in your onActivityResult() use like this:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case Constant.CAMERA_PIC_REQUEST:
try {
ExifInterface exif = new ExifInterface(imageToUploadUri.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;// Landscape
//rotate photo
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 matrix = new Matrix();
matrix.postRotate(angle);
try {
//Create the rotated .jpg file (2. file.getPath() has been used)
Bitmap correctBmp = Func.decodeSampleBitmapFromFile(imageToUploadUri.getPath(), Constant.PHOTO_WIDTH, Constant.PHOTO_HEIGHT);
correctBmp = Bitmap.createBitmap(correctBmp, 0, 0, correctBmp.getWidth(), correctBmp.getHeight(), matrix, true);
FileOutputStream fOut;
fOut = new FileOutputStream(file);
correctBmp.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
fOut.flush();
fOut.close();
//image saved successfully - add photo name into reference array. Later I will use this
//(3. file.getPath() has been used)
Image img = new Image();
img.setImageName(imgName);
img.setAbsolutePath(file.getAbsolutePath());
img.setRelativePath(imageToUploadUri);
imageArray.add(img);//save in array
} catch (FileNotFoundException e) {
Log.e("Error", e.getMessage());
} catch (IOException e) {
Log.e("Error", e.getMessage());
}
} catch (Exception e) {
//This exception is triggering.
Log.e("Error 15", e.getMessage());
}
//set this photo in a grid view for user to see
setGridView(imageArray);
break;
case Constant.POPUPFRAG:
//something else
break;
default:
//deafult
break;
}
}
}
I hope it helps you!
Android Saved Images into Gallery not Correctly Oriented
The value obtained after int getOrientationFromExif() is always the same: 1...
Don't know how to solve....
Please help me!
Can you help me to solve? The problem is inside
private PictureCallback mPicture = new PictureCallback()
{
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
#Override
public void onPictureTaken(byte[] data, Camera camera){
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
try
{
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
Bitmap myBitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
myBitmap = rotateImage(getOrientationFromExif(pictureFile), myBitmap);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 70 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
fos = new FileOutputStream(pictureFile);
fos.write(bitmapdata);
fos.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ mediaStorageDir)));
}
catch(FileNotFoundException e)
{
Log.d(TAG, "File not found: "+e.getMessage());
}
catch(IOException e)
{
Log.d(TAG, "Error accessing file: "+e.getMessage());
}
}
};
public Bitmap rotateImage(int angle, Bitmap bitmapSrc)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(bitmapSrc, 0, 0,
bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}
public int getOrientationFromExif(File imagePath)
{
int orientation = -1;
try
{
ExifInterface exif = new ExifInterface(imagePath.getAbsolutePath());
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
System.out.println("yuri"+exifOrientation);
switch (exifOrientation)
{
case ExifInterface.ORIENTATION_ROTATE_270:
orientation = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
orientation = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
orientation = 90;
break;
case ExifInterface.ORIENTATION_NORMAL:
orientation = 0;
break;
default:
break;
}
} catch (IOException e) {
Log.e(TAG, "Unable to get image exif orientation", e);
}
return orientation;
}
public static boolean isSdPresent() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
private static File getOutputMediaFile(int type){
File mediaFile = null;
if(isSdPresent() == false)
{
Log.d(TAG, "There is no Sd card. Cannot use the camera");
}
else
{
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),".");
if(!mediaStorageDir.exists())
{
if(!mediaStorageDir.mkdirs())
{
Log.d("WorldCupApp", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
if (type == MEDIA_TYPE_IMAGE)
{
mediaFile = new File(mediaStorageDir.getPath() + "IMG_"+ timeStamp + ".JPEG");
}
else
{
return null;
}
}
return mediaFile;
}
ExifInterface exif = new ExifInterface(imagePath.getAbsolutePath());
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
Problem is here always is 1 the orientation that means that doesn't need to rotate :(
When you fix your activity to landscape mode for a custom camera implementation the EXIF data will always have ORIENTATION_NORMAL for TAG_ORIENTATION. This is the same side effect that has activity.getWindowManager().getDefaultDisplay().getRotation() always returning ROTATION_90.
I fixed this by detecting the device rotation with an OrientationEventListener and then rewriting the EXIF orientation tag in my onPictureTaken callback to the proper value for the rotation.
Not sure if this behavior is device dependent, this is the behavior on a Galaxy S2 running 4.1.2.
I am developing a custom camera application and I am facing the following problem. When I try to retrieve an orientation using ExifInterface, it always returns 0 (ORIENTATION_UNDEFINED). This prevents me from rotating the image to the proper state so it can be displayed correctly.
I use a sample code for setting the camera rotation (method containing this code is called before startPreview()):
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(0, info);
screenOrientation = getWindowManager().getDefaultDisplay().getOrientation();
Log.d(TAG, "Screen orientation: " + screenOrientation);
screenOrientation = (screenOrientation + 45) / 90 * 90;
int rotation = 0;
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
rotation = (info.orientation - screenOrientation + 360) % 360;
}
else{
//back-facing camera
rotation = (info.orientation + screenOrientation) % 360;
Log.d("Test", "Calculated Rotation" + rotation);
}
params.setRotation(rotation);
camera.setParameters(params);
The way I save the taken picture:
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File photoFileDir = getFileDir();
if (!photoFileDir.exists() && !photoFileDir.mkdirs()){
Toast.makeText(context, "Cannot create directory for the taken picture", Toast.LENGTH_LONG).show();
return;
}
String pictureName = "photo_" + getPictureDate() + ".jpg";
String picturePath = photoFileDir.getPath() + File.separator + pictureName;
File pictureFile = new File(picturePath);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
try {
fos.write(data);
fos.close();
sendPictureSavedBroadcast(context, picturePath);
Toast.makeText(context, "Picture saved!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, "Picture could not be saved", Toast.LENGTH_LONG).show();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
The way I try to retrieve the picture orientation:
public static int getRotation(String absolutePath){
try{
ExifInterface exif = new ExifInterface(absolutePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
Log.d(TAG, "Toolkit, orientation: " + orientation);
switch(orientation){
case ExifInterface.ORIENTATION_NORMAL:
Log.d(TAG, "Image is properly rotated");
return 0;
case ExifInterface.ORIENTATION_ROTATE_90:
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270;
case ExifInterface.ORIENTATION_UNDEFINED:
Log.d(TAG, "Error getting orientation");
return -1;
default:
Log.d(TAG, "Error getting orientation");
return -1;
}
}
catch(Exception e){
e.printStackTrace();
}
return -1;
}
I went through all the similar topics on stackoverflow, but did not find the solution. I would appreciate any suggestions!
I want to add image capturing in my Android app where user can captures images, I'm using the following code:
public void open_camera() {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
try {
// place where to store camera taken picture
photo = this.createTemporaryFile("picture", ".jpg");
photo.delete();
} catch(Exception e) {
Toast.makeText(this, "Please check SD card! Image shot is impossible!", 10000);
}
mImageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
//start camera intent
startActivityForResult(intent,SELECT_PICTURE_CAMERA );
//Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//startActivityForResult(cameraIntent, SELECT_PICTURE_CAMERA);
}
private File createTemporaryFile(String part, String ext) throws Exception {
File tempDir= Environment.getExternalStorageDirectory();
tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
if(!tempDir.exists()) {
tempDir.mkdir();
}
return File.createTempFile(part, ext, tempDir);
}
When I retrieve
case SELECT_PICTURE_CAMERA:
if(resultCode == RESULT_OK) {
// Toast.makeText(this,"Camera" + imageReturnedIntent.getStringExtra(ZBarConstants.SCAN_RESULT), Toast.LENGTH_LONG).show();
//this.yourSelectedImage = (Bitmap) imageReturnedIntent.getExtras().get("data");
/*
ImageButton imgbtn = (ImageButton) findViewById(R.id.imageButton_Photo);
BitmapDrawable background = new BitmapDrawable(this.yourSelectedImage);
imgbtn.setBackgroundDrawable(background);
*/
try {
File f=new File(mImageUri.getPath());
ExifInterface exif = new ExifInterface(f.getPath());
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(90);
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, null);
this.yourSelectedImage = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
} catch (IOException e) {
Log.w("TAG", "-- Error in setting image");
} catch(OutOfMemoryError oom) {
Log.w("TAG", "-- OOM Error in setting image");
}
But unfortunately after doing all this, still image is not of full size and it's always landscape.
You have a small mistake in code, after you get the angle the image should rotate you need to use this value in postRotate.
Please replace the line:
mat.postRotate(90);
with:
mat.postRotate(angle);
Hope I helped you :-)
Regards,
Woody.