While developing a new update for Android application (Notebook With Categories), we faced the following issue: when a user makes a photo (snapshot) under landscape mode, the app takes this picture in portrait under grid view which is not good for image preview. This error was not common, it occurred only on some devices. We managed to solve that issue via adding a Rotate Image button. We took some research and found out that there were no clear steps described by someone else explaining how to fix that, so here we are happy to share how we handled that. In short, this article will explain how to rotate a bitmap in Android. By the end, you will understand how to rotate the image in Java. One thing should be noted – due to a canvas restrictions, it is not really possible to rotate the image by an angle other than 90 degrees, not even 45.

Step 1. Create a new screen. The screen has a flat color background, Upload Image button and Rotate Image button (button which Rotates the image by 90 degrees). Rotate Image button will rotate Image on Button Click. By the button click, we imply the stroke into a center of the button.

Create MainActivity.xml

Step 2. Add permission to Manifest file for writing to external storage:

Step 3. Edit MainActivity.java file.

Add the function of the screen button for taking image from gallery:

Here we retrieve image path:

Let’s look at the screen size in order to make the image fit the screen size:

Image processing to define image mode (landscape or portrait):

Insert the image from image file, set orientation and size of screen:

Set event function for Android rotate button:

Look at the orientation of image and rotate the image for 90 degrees and then again upload it to ImageView:

The repeated button presses will cyclically change the image orientation from 0 degrees to 90 degrees to 180 degrees to 270 degrees and to 0 degrees again. By the use of the RotateAnimation method, it is also possible to implement rotate animation.

Source code. Source code is available on github.

Read also:

• How to Choose a Mobile Development Platform?

• How Much Does It Cost to Develop a Mobile App?

• How to Integrate Payment Gateway in a Mobile App?

How to Rotate an Image in Android
5 based on 4 user ratings