I am capturing image from camera and selecting image from gallery. In samsung devices the images gets rotate after captured.
I want rotate image to straight if they are rotated.
I tried to do it but its not working.
private void onCaptureImageResult(Intent data) {
try {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".png");
FileOutputStream fo;
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
profileImage = destination;
Bitmap rotatedBitmap = modifyOrientation(thumbnail, profileImage.getAbsolutePath());
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1); //replace 100 with desired quality percentage.
byte[] byteArray1 = stream1.toByteArray();
File tempFile1 = File.createTempFile("temp", null, getCacheDir());
FileOutputStream fos1 = new FileOutputStream(tempFile1);
fos1.write(byteArray1);
if (rotatedBitmap != null) {
profileImageView.setImageBitmap(rotatedBitmap);
profileImage = tempFile1;
} else {
profileImageView.setImageBitmap(thumbnail);
profileImage = destination;
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream); //replace 100 with desired quality percentage.
byte[] byteArray = stream.toByteArray();
try {
File tempFile = File.createTempFile("temp",null, getCacheDir());
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(byteArray);
profileImage = tempFile;
Bitmap rotatedBitmap = modifyOrientation(bm,profileImage.getAbsolutePath());
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream1); //replace 100 with desired quality percentage.
byte[] byteArray1 = stream1.toByteArray();
File tempFile1 = File.createTempFile("temp",null, getCacheDir());
FileOutputStream fos1 = new FileOutputStream(tempFile1);
fos1.write(byteArray1);
if(rotatedBitmap != null) {
profileImageView.setImageBitmap(rotatedBitmap);
profileImage = tempFile1;
}
else {
profileImageView.setImageBitmap(bm);
profileImage = tempFile;
}
}
catch (IOException e)
{
}
}
EDIT:
I tried to use camera intent now and get path from intent still its not working.
private void onCaptureImageResult(Intent data) {
try {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".png");
FileOutputStream fo;
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
Bitmap rotatedBitmap = null;
// profileImage = destination;
Uri tempUri = getImageUri(getApplicationContext(),thumbnail);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
ExifInterface ei = new ExifInterface(finalFile.getAbsolutePath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotateImage(thumbnail, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotateImage(thumbnail, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotateImage(thumbnail, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
break;
}
if (rotatedBitmap != null) {
profileImageView.setImageBitmap(rotatedBitmap);
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1); //replace 100 with desired quality percentage.
byte[] byteArray1 = stream1.toByteArray();
File tempFile1 = File.createTempFile("temp", null, getCacheDir());
FileOutputStream fos1 = new FileOutputStream(tempFile1);
fos1.write(byteArray1);
profileImage = tempFile1;
} else {
profileImageView.setImageBitmap(thumbnail);
profileImage = destination;
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
What's wrong now?
Can anyone help please? What's going wrong? Thank you..