Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image from camera is null if camera is rotated

I am showing a picker to the user to choose between Gallery or Camera to select a photo. If I choose Camera, then once camera has loaded I rotate to take a Landscape photo, take the photo and click done, it returns to my app but the returned image is null. If I don't rotate the camera, the image is returned fine. What am I missing? I know the rotation causes the Activity to be rebuilt, but why wouldn't the onActivityResult contain the right information? Here is my openImage Intent:

public void openImageIntent() {

        // Determine Uri of camera image to save.
        final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyAppImages"
                + File.separator);
        root.mkdirs();
        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy-hhmmss");
        final String fname = String.format("%s.jpg", sdf.format(new Date()));
        final File sdImageMainDirectory = new File(root, fname);
        outputFileUri = Uri.fromFile(sdImageMainDirectory);

        // Camera.
        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        final PackageManager packageManager = getPackageManager();
        final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
        for (ResolveInfo res : listCam) {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            cameraIntents.add(intent);
        }

        // Filesystem.
        final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

        // Chooser of filesystem options.
        final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[] {}));

        startActivityForResult(chooserIntent, SELECT_PICTURE_REQUEST);
    }

And the onActivityResult method:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE_REQUEST) {
                final boolean isCamera;
                if (data == null) {
                    isCamera = true;
                } else {
                    final String action = data.getAction();
                    if (action == null) {
                        isCamera = false;
                    } else {
                        isCamera = true;
                    }
                }

                Uri selectedImageUri;
                if (isCamera) {
                    selectedImageUri = outputFileUri;
                } else {
                    selectedImageUri = data == null ? null : data.getData();
                }

                if (imageDelegate != null) {
                    Log.e(TAG, "imageDelegate not null: " + imageDelegate);
                    imageDelegate.gotNewImageUri(selectedImageUri);
                    imageDelegate = null;

                } else if (getSupportFragmentManager().findFragmentByTag("addofferdialog") != null) {
                    imageDelegate = (AddOfferFragment) getSupportFragmentManager().findFragmentByTag("addofferdialog");
                    Log.e(TAG, "imageDelegate is null but found fragment: " + imageDelegate);
                    Log.e(TAG, "Activity image: " + selectedImageUri);
                    imageDelegate.gotNewImageUri(selectedImageUri);
                    imageDelegate = null;
                } else {
                    Log.e(TAG, "cannot find imageDelegate!!!!");
                }

             Log.e(TAG, "selectedImageUri: " + selectedImageUri);
            }
        }
    }
like image 615
Darren Avatar asked Jun 06 '13 09:06

Darren


People also ask

Why does an image captured using camera intent gets rotated?

Most phone cameras are landscape, meaning if you take the photo in portrait, the resulting photos will be rotated 90 degrees. In this case, the camera software should populate the Exif data with the orientation that the photo should be viewed in.

How do I change the orientation of a picture on android?

Open your device's Settings app. . Select Accessibility. Select Auto-rotate screen.


2 Answers

Just save outputFileUri in onSaveInstanceState and restore it in onRestoreInstanceState other wise it will be null in orientation change.

like image 53
Arun C Avatar answered Nov 04 '22 00:11

Arun C


you have to change your manifest file

in your manifest just replace below code

<activity android:name=".CameraTestActivity"
              android:label="@string/app_name"     android:configChanges="keyboardHidden|orientation">

with your code

<activity android:name=".CameraTestActivity"
          android:label="@string/app_name">
like image 2
blganesh101 Avatar answered Nov 03 '22 23:11

blganesh101