Search code examples
androidcameraandroid-camerasamsung-mobile

Camera orientation in samsung device issue


I am successfully clicking the image and saving it in the card. It is working perfectly in all the devices, but issue is that when I am testing it in Samsung device, then as I am clicking image in the portrait mode then it is saving image by default in the landscape mode. But I am not doing any type of rotating code. So please help me. How can I solve it??

private void takePicture() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    try {
        Uri mImageCaptureUri = null;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            mImageCaptureUri = Uri.fromFile(mFileTemp);
        } else {
            mImageCaptureUri = InternalStorageContentProvider.CONTENT_URI;
        }
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                mImageCaptureUri);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
    } catch (ActivityNotFoundException e) {

        Log.d("", "cannot take picture", e);
    }
}

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

    if (resultCode != RESULT_OK) {

        return;
    }

    Bitmap bitmap;

    switch (requestCode) {

    case REQUEST_CODE_TAKE_PICTURE:

        startCropImage();
        break;
    case REQUEST_CODE_CROP_IMAGE:

        String path = data.getStringExtra(CropImage.IMAGE_PATH);
        if (path == null) {

            return;
        }

        bitmap = BitmapFactory.decodeFile(mFileTemp.getPath());
        resizedBitmap = ScalingUtilities.createScaledBitmap(bitmap, 320,
                320, ScalingLogic.CROP);
        imgPreview.setImageBitmap(resizedBitmap);
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}


private void startCropImage() {

    Intent intent = new Intent(this, CropImage.class);
    intent.putExtra(CropImage.IMAGE_PATH, mFileTemp.getPath());
    intent.putExtra(CropImage.SCALE, true);

    intent.putExtra(CropImage.ASPECT_X, 2);
    intent.putExtra(CropImage.ASPECT_Y, 2);

    startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);
}

I added following permissions in the manifest.

   <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Solution

  • I solved my bug, actually I use one trick. I took the manufactured name programtically, like this

     String device_name = Build.MANUFACTURER;
    

    After that I saved it in the sharedpreferences object.

     deviceIdPreferences = getSharedPreferences("USER_INFO", Context.MODE_PRIVATE);
        SharedPreferences.Editor   editor = deviceIdPreferences.edit();
        editor.putString("Device_id",deviceId);
        editor.putString("device_name",device_name);
        editor.commit();
    

    After that in the destination java file, I extrat the value from sharedpreference.

    preferences = this.getSharedPreferences(
                "USER_INFO", Context.MODE_PRIVATE);
        device_name = preferences.getString("device_name", "Empty");
    

    and then

        mBitmap = getBitmap(mImagePath);
        if(device_name.equals("samsung")){
    
                switch (Integer.parseInt(gotOrientation)) {
    
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        mBitmap = Util.rotateImage(mBitmap, 90);
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        mBitmap = Util.rotateImage(mBitmap, 90);
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        mBitmap = Util.rotateImage(mBitmap, 90);
                        break;
                    default:
                        mBitmap = Util.rotateImage(mBitmap, 90);
                        break;
                }
    
            }
    
    
    
    
     private Bitmap getBitmap(String path) {
    
        Uri uri = getImageUri(path);
        InputStream in = null;
        try {
            in = mContentResolver.openInputStream(uri);
    
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
    
            BitmapFactory.decodeStream(in, null, o);
            in.close();
    
            int scale = 1;
            if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }
    
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            in = mContentResolver.openInputStream(uri);
            Bitmap b = BitmapFactory.decodeStream(in, null, o2);
            in.close();
    
            return b;
        } catch (FileNotFoundException e) {
            Log.e(TAG, "file " + path + " not found");
        } catch (IOException e) {
            Log.e(TAG, "file " + path + " not found");
        }
        return null;
    }