9

I have a very simple layout with an ImageView. My application opens the camera, saves an image, and then displays the image in the ImageView with BitmapFactory.decodeFile. The only issue is that it's rotated. I understand that a) this is due to the phone's camera defaulting to landscape, so this needs to be handled in code and b) image processing must be done in a separate thread from the UI.

The Android training documentation seems to be geared toward loading images from resource id's instead of file paths. This just throws a wrench in things as I'm able to follow the tutorials up to a point but ultimately have trouble reconciling the differences.

I'm certainly new at this so if someone could just give me a high-level overview of what needs to be done to accomplish this, I'd really appreciate it. The following code is where I'm currently adding the bitmap to the ImageView. I assume that my call to the separate thread would go in onCreate?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_image);

    Bitmap bm = BitmapFactory.decodeFile(MainActivity.image_path);

    displayImageView = (ImageView) findViewById(R.id.myImageView);

    displayImageView.setImageBitmap(bm);
}
2
  • So you want to only rotate the Image? Commented Sep 1, 2014 at 3:01
  • Correct. The ImageView itself is properly oriented. It's just that the image inside it is always displayed 90 degrees counter-clockwise. The file, however, is oriented properly so I just need to rotate it for the application. Commented Sep 1, 2014 at 3:10

4 Answers 4

9

Use ExifInterface for rotate

picturePath = getIntent().getStringExtra("path");
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
ExifInterface exif = new ExifInterface(picturePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
    matrix.postRotate(90);
    break;
case ExifInterface.ORIENTATION_ROTATE_180:
    matrix.postRotate(180);
    break;
case ExifInterface.ORIENTATION_ROTATE_270:
    matrix.postRotate(270);
    break;
default:
    break;
}

myImageView.setImageBitmap(bitmap);
bitmap.recycle();

Note : Here picturePath is selected Image's path from Gallery

5
  • This is working but it raises one additional issue that I just want to confirm. I'm doing this in a separate thread so I can't actually render the bitmap until onPostExecute. Again - it's working great but I just want to be sure that this isn't creating an additional bitmap when passed into onPostExecute. So I understand, Java is only passing a reference, so the entire process really only creates one bitmap. Is this correct? Commented Sep 1, 2014 at 17:30
  • instead of passing the bitmap pass picturePath and processed the bitmap in onPostExecute.
    – Kaushik
    Commented Sep 2, 2014 at 4:17
  • onPostExecute is run on the UI thread, though. It's entirely possible that I have an incorrect understanding of this but I was under the impression that all bitmap processes needed to be completed in a non-UI thread. It did just dawn on me, however, that I'm only loading a scaled version of the bitmap and it's being loaded from memory - not over the network or anything. Would this be ok to complete in the UI thread as long as I'm not loading the entire, unscaled image? Commented Sep 2, 2014 at 19:29
  • move the code into onActivityResult(...) and no need of scaled bitmap
    – Kaushik
    Commented Sep 3, 2014 at 4:13
  • Still getting the hang of the device monitor but it did seem that there were some memory savings using a sampled image as opposed to unsampled. The file is generated by the camera so the original bitmap is fairly large. Commented Sep 3, 2014 at 14:06
2

I was able to get it working with the following approach. I think you could do this without the AsyncTask but it seems that best practice states you should perform image processing in a non-UI thread (please feel free to correct - I'm quite new at this).

An improvement which could be made to this would be to handle the scaling in a more granular way. inSampleSize is a great tool but it will only scale by powers of 2. Another improvement would be to initially only read the meta/EXIF data into bmOriginal as the only use of this variable is to retrieve the height and width.

ImageView XML - pretty much standard. android:layout_width/height set to fill_parent.

From the activity's java file -

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_image);

    new ImageRotator().execute(MainActivity.image_path);
}

private class ImageRotator extends AsyncTask<String, Void, Bitmap>{
    protected Bitmap doInBackground(String... image_path){

        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inSampleSize = 4;
        Bitmap bmOriginal = BitmapFactory.decodeFile(image_path[0], bitmapOptions);

        Matrix matrix = new Matrix();

        matrix.postRotate(90);

        bmOriginal = Bitmap.createBitmap(bmOriginal, 0, 0, bmOriginal.getWidth(), bmOriginal.getHeight(), matrix, true);

        return bmOriginal;
    }

    protected void onPostExecute(Bitmap result) {
        myImageView = (ImageView) findViewById(R.id.myImageView);

        myImageView.setImageBitmap(result);
    }
}

The scaling process was found here and there's a great explanation of AsyncTask's features here.

Thanks so much to all who responded and again - please feel free to let me know if this is way off base or even if there's just a better way to do this.

1

Why bother to rotate the image. As you said "The ImageView itself is properly oriented. It's just that the image inside it is always displayed 90 degrees counter-clockwise. " , then just make the ImageView rotate 90 degrees clockwise, the image will display correctly.

    displayImageView.setRotation(90);
3
  • This does properly orient the image but now there's a lot of white space around it. My android:layout_width/height are set to fill_parent. It seems that the height of the final ImageView is the width of my display. I'm assuming that the rotate happens after the activity creates the view. Is there a way to "refresh" this and have it re-apply fill_parent? Thanks. Commented Sep 1, 2014 at 3:29
  • @user2864874 Can you post a screenshot in your original question? Commented Sep 1, 2014 at 5:43
  • @user2864874 If you set the ImageView's layout_height/width fill/match_parent, the space outside of the image should be transparent. The white space you mentioned should be the color of the underground view. Maybe you need to scale the ImageView to fill the space?
    – wrkwrk
    Commented Sep 1, 2014 at 5:55
0

You can rotate an image using a matrix or set the rotation in your xml. If you want more control however, you can check out this tutorial written for rotating images and saving to disk:

How to rotate an image in android

I hope that helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.