Android Question [SOLVED] B4X - Set B4XBitmap to an image from a photo that was taken from camera using SimpleMediaManager or an other way

hatzisn

Expert
Licensed User
Longtime User
Just when I was sure I had finished my application I noticed that the photos it takes in some phones is rotated to the side. Searching the forum I found that Simple Media Manager can manage this situation. I tried to add it but I was not successful in getting the imageview to a bmp. I get the error that bmp is not initialized. What can I do?

I use this code:

B4X:
    'pnlCurrent is not visible
    smm.SetMediaFromFile(pnlCurrent, xui.Defaultfolder , "1.jpg", "image/*", Null)
   
    Dim iCountViews As Int
    For Each v As B4XView In pnlCurrent.GetAllViewsRecursive
        iCountViews = iCountViews + 1
    Next
    Log(iCountViews)
   
    If pnlCurrent.GetView(0) Is ImageView Then
        Log("ImageView")
    End If
    If pnlCurrent.GetView(0) Is Panel Then
        Log("Panel")
    End If

   
    Dim bmp As B4XBitmap = pnlCurrent.GetView(0).GetBitmap
'    Dim out As OutputStream = File.OpenOutput(xui.Defaultfolder, "2.jpg", False)
'    bmp.WriteToStream(out, 100, "JPG")
'    out.Flush
'    out.Close
 

hatzisn

Expert
Licensed User
Longtime User

I have figured it out. Here is the code...

B4X:
    pnlCurrent.Visible = False
    Try
        File.Delete(xui.DefaultFolder, "2.jpg")
    Catch
        Log(LastException)
    End Try
    Log("File.Exists=" & File.Exists(xui.DefaultFolder , "1.jpg"))
    smm.SetMediaFromFile(pnlCurrent, xui.DefaultFolder , "1.jpg", "image/jpeg", CreateMap(smm.REQUEST_RESIZE_MODE:"FILL_HEIGHT", smm.REQUEST_CALLBACK: Me))
    
    Wait For (pnlCurrent) SMM_MediaReady (Success As Boolean, Media As SMMedia)
    Dim b4xmvp As B4XBitmap = Media.Media

    Dim Out As OutputStream = File.OpenOutput(xui.DefaultFolder, "2.jpg", False)
    b4xmvp.WriteToStream(Out, 100, "JPEG")
    #if b4a
    Out.Flush
    #end if
    Out.Close
    
    Dim bmp As B4XBitmap = b4xmvp
    Dim xDim As Int = 320
    Dim yDim As Int = bmp.Height * xDim / bmp.Width
    bmp = xui.LoadBitmapResize(xui.DefaultFolder, "2.jpg", xDim, yDim, True)
    Return bmp
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…