0

I search for quite long time to get the solution about using camera intent to rotate the image before saving into sd card. i try to capture a photo in portrait and go into sd card file path to view it show as landscape. Any got a idea how to solve this? My Code so far as below :-

 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);   

    @Override

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {


    Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
                if(cursor != null && cursor.moveToFirst())
                {
                    do {
                        uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));

                    photoPath = uri.toString();

Matrix matrix = new Matrix();

        ExifInterface exifReader = null;
        try {
            exifReader = new ExifInterface(photoPath);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }// Location of your image

        int orientation = exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        if (orientation ==ExifInterface.ORIENTATION_NORMAL) {

        // Do nothing. The original image is fine.
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {

               matrix.postRotate(90);

        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {

               matrix.postRotate(180);

        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {

               matrix.postRotate(270);

        }
                }while(cursor.moveToNext());
                cursor.close();
            }       
}
micky
  • 235
  • 2
  • 4
  • 16
  • refer this answer http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android – Shruti Oct 25 '13 at 04:54
  • see this answer too http://stackoverflow.com/questions/6069122/camera-orientation-problem-in-android – Shruti Oct 25 '13 at 04:55
  • i'm new to android. May i know any idea how to merge into my code? on onActivityResult? – micky Oct 25 '13 at 04:58

3 Answers3

1

Use the code below to rotate your image:

Uri imageUri = intent.getData();
            String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
            Cursor cur = managedQuery(imageUri, orientationColumn, null, null, null);
            int orientation = -1;
            if (cur != null && cur.moveToFirst()) {
                orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
            }  
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);
CodeWarrior
  • 5,026
  • 6
  • 30
  • 46
0

I recently faced a similar problem and it took me quite some time to figure this out. This might not be a very good solution but it worked for me quite well.

Have a look at the following code. Hope this helps:

Camera Snippet

This snippet also contains autofocus enabling/disabling, shutter sound etc. Enjoy!!

Ankit Popli
  • 2,809
  • 3
  • 37
  • 61
  • thanks for sharing this. between u got any sample with call intent ACTION_IMAGE_CAPTURE? – micky Oct 25 '13 at 06:14
  • nope.. but you can find many such examples/tutorial for that. Here's one from developer.android.com: http://developer.android.com/training/camera/photobasics.html and http://developer.android.com/guide/topics/media/camera.html#intent-receive – Ankit Popli Oct 25 '13 at 06:22
  • i mean example call intent ACTION_IMAGE_CAPTURE with rotate proper way before saving in sd card. – micky Oct 25 '13 at 06:31
  • ok.. haven't tried that.. so can't say.. will get back to you if i find anything. and please keep me updated too. :) – Ankit Popli Oct 25 '13 at 06:44
-1

Hi have a look at this below code. before saving your captured image do the following process. it will save the images in portrait mode. hope this will help you.

int rotation = -1;
 rotation = ((WindowManager)getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay().getOrientation();



    Matrix rotator = new Matrix();
    switch (rotation) {
    case (Surface.ROTATION_0):
        break;
    case (Surface.ROTATION_90):
        rotator.postRotate(270);
        break;
    case (Surface.ROTATION_180):
        rotator.postRotate(180);
        break;
    case (Surface.ROTATION_270):
        rotator.postRotate(90);
        break;


    // screen_{width,height} are applied before the rotate, so we don't
    // need to change them based on rotation.
    bmp_ss = Bitmap.createBitmap(bmp_ss, 0, 0, screen_width, screen_height, rotator, false);