Android Question File Rotation... Can't Get It Done!


Active Member
Licensed User
This is weird.

I can rotate the bitmap ok, but the actual container doesn't change.

I want to rotate a bitmap that's in portrait mode, to landscape, and save it as that landscape size. It appears that when I rotate the bitmap, and save it, it's saved as the horizontal picture, inside a portrait container, much of which is black above and below the rotated pic. So, in what's saved, I end up with the rotated image the width of which is the width of the portrait container and the height is only a small portion of the portrait picture, but the rotated picture is in the correct proportions.

I've tried several of the things I found on the site, but still come up with the same result. Here's where I am now:

Sub RotateView(v As View, bmp As Bitmap, degrees As Float)
    Dim c As Canvas
    Dim R As Rect
    'Dim imgratio As Long
    Dim hh2,hw2 As Int
    hh2 = v.Height
    hw2 = v.Width


    Dim out As OutputStream
    Dim rfile As String
    rfile = "rotatetemp.jpg"
    out = File.OpenOutput(File.DirInternal, rfile, False)
    c.Bitmap.WriteToStream(out, 100, "JPEG")

End Sub

Sub SetPreviewLayout  '(bwidth As Int, bheight As Int)

    'ivPreview.Height = bwidth
    'ivPreview.Width = bheight
    ivPreview.Top = (btnShoot.Top / 2) - (ivPreview.Height / 2)
    ivPreview.Left = iv.Left + ((iv.Width / 2) - (ivPreview.Width / 2) )
    btnRotateLeft.Top = (ivPreview.Top + ivPreview.Height / 2) - btnRotateLeft.Height / 2
    btnRotateRight.Top = btnRotateLeft.Top
    btnRotateLeft.Left = ivPreview.Left - btnRotateLeft.Width - 20dip
    btnRotateRight.Left = ivPreview.Left + ivPreview.Width +  20dip   
End Sub

Sub ResizePreviewPhoto (Dir As String, FileName As String)
    Dim bm2 As Bitmap
    bm2.InitializeSample(Dir, FileName, 100dip, 100dip)
    hip = bm2.Height
    wip = bm2.Width
    ivPreview.Bitmap = bm2
        If hip = wip Then 'It's Square
            ivPreview.Height = 100dip
            ivPreview.Width = 100dip
        Else If hip > wip Then 'It's Portrait
            imageratio = (wip / hip)
            ivPreview.Height = 100dip
            ivPreview.Width = ivPreview.Height  * imageratio  '- 20dip
        Else 'It's Landscape
            imageratio = (hip / wip)
            ivPreview.Width = 100dip 'pnl.Width '- 40dip
            ivPreview.Height = ivPreview.Width * imageratio
        End If
End Sub

I don't know what to do next. This SHOULD work, shouldn't it? If not, where'd I go wrong here?


Licensed User
Hi Erel,

I have used your RotateImage function from post #8, but when I run my app on some devices (e.g. LG G Pad tablet or Sony Xperia E3), I get OutOfMemoryError. On the other hand, on some another devices (e.g. Sony Xperia Z3 or high end Samsung phone) works smoothly.

Downsampling image due to lack of memory.
main_rotateimage (B4A line: 903)
NewImage = bmp.RunMethod("createBitmap", Array
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(
at anywheresoftware.b4j.object.JavaObject.RunMethod(
at b4a.ArtAngels.main._rotateimage(
at b4a.ArtAngels.main._showcroppingenvironment(
at b4a.ArtAngels.main._btncropimage_click(
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(
at anywheresoftware.b4a.BA.raiseEvent2(
at anywheresoftware.b4a.BA.raiseEvent2(
at anywheresoftware.b4a.BA.raiseEvent(
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(
at android.view.View.performClick(
at android.view.View$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.OutOfMemoryError
at Method)
... 23 more

My aim is to rotate high resolution images 90 degrees clockwise.

Please, how can I avoid OutOfMemoryError?

Thank you.
Upvote 0


Active Member
Licensed User
It's really tough to do. You're taking a high res picture and loading it into memory. Then you're rotating that so you're creating another bitmap in memory. They're both huge in size. And there's a lot going on in the phone as well. I bet that the phones that work have more internal memory than the group that doesn't. 16GB on a phone isn't the amount of usable memory you have to work with... it's just the overall storage space.

This is a guess, but I remember that when I was playing with this, certain devices worked and others didn't. The ones with more workable memory seemed not to falter.
Upvote 0


Licensed User
Hi Shadow&Max,

thank you for diagnostics.

Please, is there other approach, how can I rotate image outside of memory?

My code is crashing on line:

NewImage = bmp.RunMethod("createBitmap", Array(original, 0, 0, original.Width, original.Height, matrix, True))​

... where NewImage is global variable.

Thank you.
Upvote 0