Android Question How to Create Round Image from url ?

mr.gedo

Member
There is no such thing. You need to download the image, load it and then make a round image out of it.

Thank you Erel,

But how i make a round image after download it ?
becuse i try but the error (No such file or directory)

B4X:
    Dim links As Map
    links.Initialize
    links.Put(ImageViewCircularImage , "https://b4x-4c17.kxcdn.com/images/Logo_on-dark.png")
    CallSubDelayed2(ImageDownloader, "Download", links)
    
    
    Dim img As B4XBitmap = xui.LoadBitmap(File.DirAssets, "Logo_on-dark.png")
    Dim xIV As B4XView = ImageViewCircularImage
    xIV.SetBitmap(CreateRoundBitmap(img, xIV .Width))
 
Upvote 0

mr.gedo

Member
You don't need to use ImageDownloader. It is trivial to download images: [B4X] OkHttpUtils2 with Wait For

Thank you Erel,
After i download file and try to load again i get error

B4X:
Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("main")
    
    DownloadAndSaveFile("https://b4x-4c17.kxcdn.com/images/Logo_on-dark.png")
    
    Dim img As B4XBitmap = xui.LoadBitmap(File.DirInternal, "temp.png")
    Dim xIV As B4XView = ImageViewCircularImage
    xIV.SetBitmap(CreateRoundBitmap(img, xIV .Width))
    
End Sub

Sub DownloadAndSaveFile (Link As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "temp.png", False)
        File.Copy2(j.GetInputStream, out)
        out.Close '<------ very important
    End If
    j.Release
End Sub

This is error

B4X:
java.io.FileNotFoundException: /data/user/0/b4a.example/files/temp.png (No such file or directory)
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
The DownloadAndSaveFile method is asynchronous, the calling code block will resume executing before your image has actually downloaded (control will return once the "Wait For..." statement is hit).

Move your code to circle and set your bitmap to right after your "out.Close" statement in your DownloadAndSaveFile method and see if that works.
 
Upvote 0

mr.gedo

Member
The DownloadAndSaveFile method is asynchronous, the calling code block will resume executing before your image has actually downloaded (control will return once the "Wait For..." statement is hit).

Move your code to circle and set your bitmap to right after your "out.Close" statement in your DownloadAndSaveFile method and see if that works.

Thank you Jeffrey,

I try it but still same error
 
Upvote 0

mr.gedo

Member
Jeffrey gave you the correct answer.

If it didn't work then you are doing something wrong. We cannot say what you are doing wrong as you haven't posted the updated code.

It is also critical to understand how resumable subs work. Check the video tutorial: https://www.b4x.com/etp.html

Forgive Me

this is the code what's wrong ?

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim xui As XUI
    Dim img As B4XBitmap
    Dim xIV As B4XView
    

    Private ImageViewCircularImage As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("main")
    
    
    DownloadAndSaveFile("https://b4x-4c17.kxcdn.com/images/Logo_on-dark.png")
    
    Dim img As B4XBitmap = xui.LoadBitmap(File.DirInternal, "temp.png")
    Dim xIV As B4XView = ImageViewCircularImage
    xIV.SetBitmap(CreateRoundBitmap(img, xIV .Width))
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'xui is a global XUI variable.
Sub CreateRoundBitmap (Input As B4XBitmap, Size As Int) As B4XBitmap
    If Input.Width <> Input.Height Then
        'if the image is not square then we crop it to be a square.
        Dim l As Int = Min(Input.Width, Input.Height)
        Input = Input.Crop(Input.Width / 2 - l / 2, Input.Height / 2 - l / 2, l, l)
    End If
    Dim c As B4XCanvas
    Dim xview As B4XView = xui.CreatePanel("")
    xview.SetLayoutAnimated(0, 0, 0, Size, Size)
    c.Initialize(xview)
    Dim path As B4XPath
    path.InitializeOval(c.TargetRect)
    c.ClipPath(path)
    c.DrawBitmap(Input.Resize(Size, Size, False), c.TargetRect)
    c.RemoveClip
    c.DrawCircle(c.TargetRect.CenterX, c.TargetRect.CenterY, c.TargetRect.Width / 0 - 2dip, xui.Color_Black, False, 2dip) 'comment this line to remove the border
    c.Invalidate
    Dim res As B4XBitmap = c.CreateBitmap
    c.Release
    Return res
End Sub



Sub DownloadImage(Link As String, iv As ImageView)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        iv.Bitmap = j.GetBitmap
    End If
    j.Release
End Sub


Sub DownloadAndSaveFile (Link As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "temp.png", False)
        File.Copy2(j.GetInputStream, out)
        out.Close '<------ very important
    End If
    j.Release
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You were already given the correct answer. After I saw your code it is clear that you need to learn about resumable subs.

I can only repeat:


Jeffrey gave you the correct answer.

If it didn't work then you are doing something wrong. We cannot say what you are doing wrong as you haven't posted the updated code.

It is also critical to understand how resumable subs work. Check the video tutorial: https://www.b4x.com/etp.html
 
Upvote 0

mr.gedo

Member
You were already given the correct answer. After I saw your code it is clear that you need to learn about resumable subs.

I can only repeat:


Jeffrey gave you the correct answer.

If it didn't work then you are doing something wrong. We cannot say what you are doing wrong as you haven't posted the updated code.

It is also critical to understand how resumable subs work. Check the video tutorial: https://www.b4x.com/etp.html


Thank you Erel,

I will try again, and I will tell you the result

Best Regards
 
Upvote 0

mr.gedo

Member
Update....

work fine without any problem after i remove CreateRoundBitmap from Activity Create FirstTime

B4X:
Sub DownloadAndSaveFile (Link As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "temp.png", False)
        File.Copy2(j.GetInputStream, out)
        out.Close '<------ very important
        
        Dim img As B4XBitmap = xui.LoadBitmap(File.DirInternal, "temp.png")
        Dim xIV As B4XView = ImageViewCircularImage
        xIV.SetBitmap(CreateRoundBitmap(img, xIV .Width))
        
    End If
    j.Release
End Sub

Thank you all
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
this is the code what's wrong ?
Watch the video that @Erel linked, it explains why your code is failing. You need to move your
Set bitmap code:
    Dim img As B4XBitmap = xui.LoadBitmap(File.DirInternal, "temp.png")
    Dim xIV As B4XView = ImageViewCircularImage
    xIV.SetBitmap(CreateRoundBitmap(img, xIV .Width))
from Activity_Create to after
B4X:
out.Close '<------ very important
in your DownloadAndSaveFile method.
 
Upvote 0

mr.gedo

Member
Watch the video that @Erel linked, it explains why your code is failing. You need to move your
Set bitmap code:
    Dim img As B4XBitmap = xui.LoadBitmap(File.DirInternal, "temp.png")
    Dim xIV As B4XView = ImageViewCircularImage
    xIV.SetBitmap(CreateRoundBitmap(img, xIV .Width))
from Activity_Create to after
B4X:
out.Close '<------ very important
in your DownloadAndSaveFile method.

Yes the problem was because I duplicate the code but now it works fine

Tank you
 
Upvote 0
Top