I'm creating an android app that utilizes the camera to take pictures and then sends them to a server based on user input. I'm currently having some problems with the camera intent. My main problems are :
Getting a picture that seems to be rotated when compared to the position it was taken.
When I try to fix this Rotation I get an OutOfMemoryError
So mainly I need to make sure that the orientation of the picture doesn't change and that I don't get an OutOfMemoryError.
Here is the function that uses the camera Intent to take the picture.
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(takePictureIntent, actionCode);
}
And this function is used to rotate the image and save it at that rotation.
private void getRotate() {
String imagePath ="";
int rotate = 0;
File f = null;
try {
f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
imagePath = f.getAbsolutePath();
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
Bitmap p = BitmapFactory.decodeFile(imagePath);
Matrix m = new Matrix();
m.setRotate(rotate);
Bitmap _bitmapScaled = Bitmap.createBitmap(p, 0, 0, p.getWidth()/2,p.getHeight(), m, false);
f.delete();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f1 = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
try {
f1.createNewFile();
FileOutputStream fo = new FileOutputStream(f1);
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//write the bytes in file
}
And finally, this is my onActivityResult method :
protected void onActivityResult(int requestCode, int resultCode, Intent data){
getRotate();
File f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
mImageView.setImageBitmap(BitmapFactory.decodeFile(f.getAbsolutePath()));
}
I've been trying to solve this problem for a long time and I would appreciate it if I could get some help. Thanks!