B4J Question How to save an image downloaded from a link using SMM to disk?

oleg_

Member
Licensed User
SMM is a really great and flexible tool. But I still can’t figure out how to save an image already downloaded to the Panel using SMM to disk. Please tell me how to do this.

Or will I have to download the same image a second time from the same link, now using HttpJob?

That is, I want to do the operation MediaManager.SetMediaFromFile, but only in reverse. Not from file to panel, but from panel to file. Is this possible?

Thank you for your time.

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Pane1 As B4XView
    Private MediaManager As SimpleMediaManager
End Sub

Public Sub Initialize
    MediaManager.Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

Private Sub Button1_Click
    MediaManager.SetMediaWithExtra(Pane1, "https://b4x-4c17.kxcdn.com/images3/code.png", "", CreateMap(MediaManager.REQUEST_CALLBACK: Me))
    Wait For (Pane1) SMM_MediaReady (Success As Boolean, Media As SMMedia)
    If Success Then
        Log ("success")
        Private j As HttpJob
        j.Initialize("", Me)
        j.Download("https://b4x-4c17.kxcdn.com/images3/code.png")    'download the same file from the same link again :(
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Private out As OutputStream = File.OpenOutput(File.DirApp, "01.png", False)
            File.Copy2(j.GetInputStream, out)
            out.Close
        End If
    Else
        Log ("failure")
        Return
    End If
End Sub
 
Last edited:
Solution
I adjusted it with the tip TILogistic:

To use xui.DefaultFolder in b4j you must define the folder name with the command xui.SetDataFolder("nameFolder")

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Pane1 As B4XView
    Private MediaManager As SimpleMediaManager
End Sub

Public Sub Initialize
    MediaManager.Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    xui.SetDataFolder("nameFolder")
End Sub


Private Sub Button1_Click
    MediaManager.SetMediaWithExtra(Pane1, "https://b4x-4c17.kxcdn.com/images3/code.png", "", CreateMap(MediaManager.REQUEST_CALLBACK: Me))

    Wait For (Pane1) SMM_MediaReady (Success As...

Lucas Siqueira

Active Member
Licensed User
Longtime User
Erel:



B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Pane1 As B4XView
    Private MediaManager As SimpleMediaManager
End Sub

Public Sub Initialize
    MediaManager.Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub


Private Sub Button1_Click
    MediaManager.SetMediaWithExtra(Pane1, "https://b4x-4c17.kxcdn.com/images3/code.png", "", CreateMap(MediaManager.REQUEST_CALLBACK: Me))

    Wait For (Pane1) SMM_MediaReady (Success As Boolean, Media As SMMedia)

    If Success Then

        Log ("success")
        
        'try this
        'Dim bmp As B4XBitmap = Pane1.GetView(0).GetBitmap
        
        'or that
        'Dim bmp As B4XBitmap = Pane1.GetView(0).GetView(0).GetBitmap
        
        
        Dim Out As OutputStream
        Out = File.OpenOutput(File.DirApp, "01.png", False)
        bmp.WriteToStream(Out, 100, "PNG")
        Out.Close
        
    Else

        Log ("failure")

        Return

    End If

End Sub
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Other Option.
B4X:
    MediaManager.Initialize
    MediaManager.SetMediaWithExtra(Pane1, "https://b4x-4c17.kxcdn.com/images3/code.png", "", CreateMap(MediaManager.REQUEST_CALLBACK: Me))

B4X:
Public Sub SMM_MediaReady (Success As Boolean, Media As SMMedia)
    If Success Then
        If Media.Media Is B4XBitmap Then
            B4XImageView1.Bitmap = Media.Media 'just to check it
            Dim Out As OutputStream = File.OpenOutput(xui.DefaultFolder, "01.png", False)
            Media.Media.As(B4XBitmap).WriteToStream(Out, 100, "PNG")
            Out.Close
        End If
    End If
End Sub

1710030976004.png


Check Defaultfolder:
1710031306231.png
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
I adjusted it with the tip TILogistic:

To use xui.DefaultFolder in b4j you must define the folder name with the command xui.SetDataFolder("nameFolder")

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Pane1 As B4XView
    Private MediaManager As SimpleMediaManager
End Sub

Public Sub Initialize
    MediaManager.Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    xui.SetDataFolder("nameFolder")
End Sub


Private Sub Button1_Click
    MediaManager.SetMediaWithExtra(Pane1, "https://b4x-4c17.kxcdn.com/images3/code.png", "", CreateMap(MediaManager.REQUEST_CALLBACK: Me))

    Wait For (Pane1) SMM_MediaReady (Success As Boolean, Media As SMMedia)

    If Success Then

        Log ("success")

        If Media.Media Is B4XBitmap Then
            Dim bmp As B4XBitmap = Media.Media
     
            Dim Out As OutputStream
            Out = File.OpenOutput(xui.DefaultFolder, "01.png", False)
            bmp.WriteToStream(Out, 100, "PNG")
            Out.Close
         End If

    Else

        Log ("failure")

        Return

    End If

End Sub
 
Last edited:
Upvote 0
Solution

TILogistic

Expert
Licensed User
Longtime User
Other Option
CreateMap(MediaManager.REQUEST_CALLBACK: MyClass(Panel1))
see:
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Other Option
CreateMap(MediaManager.REQUEST_CALLBACK: MyClass(Panel1))
see:
REQUEST_CALLBACK
Initializing the container panel is only when you want to perform other functionalities.
Example: in videos, etc.

Here is a simple example through a class.
EDIT
B4X:
    xui.SetDataFolder("SMMExample") 'Only B4J
    Log(xui.DefaultFolder)
    
    Dim MyCallBack As ClsRequestCallBack
    MyCallBack.Initialize(Pane1)
    Dim Extra As Map = CreateMap(MediaManager.REQUEST_CALLBACK: MyCallBack)
    MediaManager.SetMediaWithExtra(Pane1, "https://b4x-4c17.kxcdn.com/images3/code.png", "", Extra)
Or
B4X:
    xui.SetDataFolder("SMMExample") 'Only B4J
    Log(xui.DefaultFolder)
      
    Dim MyCallBack As ClsRequestCallBack
    Dim Extra As Map = CreateMap(MediaManager.REQUEST_CALLBACK: MyCallBack.Initialize(Pane1))
    MediaManager.SetMediaWithExtra(Pane1, "https://b4x-4c17.kxcdn.com/images3/code.png", "", Extra)

ClsRequestCallBack
B4X:
Sub Class_Globals
    Private xui As XUI
    Private MediaPanel As B4XView
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize (SMMPanel As B4XView) As Object
    MediaPanel = SMMPanel
    Return Me
End Sub

Sub SMM_MediaReady (Success As Boolean, Media As SMMedia)
    If Success Then
        If Media.Media Is B4XBitmap Then
            Dim Out As OutputStream = File.OpenOutput(xui.DefaultFolder, "Media1.png", False)
            Media.Media.As(B4XBitmap).WriteToStream(Out, 100, "PNG")
            Out.Close
        End If

        If MediaPanel.GetView(0).GetView(0).GetBitmap Is B4XBitmap Then
            Dim B4XBitmap1 As B4XBitmap =  MediaPanel.GetView(0).GetView(0).GetBitmap
            Dim Out As OutputStream = File.OpenOutput(xui.DefaultFolder, "Media2.png", False)
            B4XBitmap1.WriteToStream(Out, 100, "PNG")
            Out.Close
        End If
    End If
End Sub
1710044163437.png
 
Last edited:
Upvote 1
Top