Himanshu
Himanshu

Reputation: 1086

How to get the orientation type when capturing image via Intent?

I am stuck with a problem, when capturing image via intent.

I am using the default intent code for capturing image. as follows.

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                 startActivityForResult(intent, TAKE_PHOTO_CODE);

and receiving it in onActivityResult as follow.

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if( TAKE_PHOTO_CODE==requestCode){

        try {
             Bundle extras = data.getExtras();
             Bitmap mImageBitmap = (Bitmap) extras.get("data");
             selectedImagePath = saveImageInfo(mImageBitmap, "image");
             ExifInterface exif = new ExifInterface(selectedImagePath);
            Toast.makeText(getBaseContext()," "+exif.getAttribute(ExifInterface.TAG_ORIENTATION), Toast.LENGTH_SHORT).show();

         try {
                showDialog(DIALOG_UPLOADING);

            } catch (Exception e) {

                e.printStackTrace();
            }
          } catch (Exception e) {

          e.printStackTrace();
          Toast.makeText(getApplicationContext(), "No image selected.. ", Toast.LENGTH_SHORT).show();
        }
    }

}

I want the captured image orientation in this onActivityResult method, i have search and about it and found the ExifInterface, but it does not work for me, I am testing on LG Optimus Android device, it always returns 0 for me.

The problem is after capturing image it is rotated. Any Solution?

Upvotes: 4

Views: 1186

Answers (1)

Hitesh Sondhi
Hitesh Sondhi

Reputation: 156

You can get the rotation by using the method and then rotate the image according to the result -

private int getDisplayRotation() {
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    switch (rotation) {
        case Surface.ROTATION_0: return 0;
        case Surface.ROTATION_90: return 90;
        case Surface.ROTATION_180: return 180;
        case Surface.ROTATION_270: return 270;
    }
    return 0;
}

to get rid of lots of device specific problems, I created camera from scratch You can get the code from https://github.com/vinsol/expense-tracker/tree/master/src/com/vinsol/expensetracker/cameraservice

and then call it using the following method

private void startCamera() {
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
        Intent camera = new Intent(this, Camera.class);
        File file = new File("123.jpg");
        Log.d("camera file path "+file.toString() );
        camera.putExtra("FullSizeImagePath", file.toString());
        startActivityForResult(camera, PICTURE_RESULT);
    } else {
        Toast.makeText(this, "sdcard not available", Toast.LENGTH_LONG).show();
    }
}

Upvotes: 1

Related Questions