Android Question wait for problem

gregorio_adrian_gimenez

Active Member
Licensed User
Hello everyone!
I have a problem with the wait for.
For some reason you are not loading the images correctly in this code that completes a customlistview.
Use a wait to download an image in firebase, if the succes is false, call another function to download the image with downloadimage.
I am downloading the last image or loading the image in the same bitmap, how can I solve it?
I need to have the wait for to verify if I have an image first in firebase.

thanks for the suggestions you can give me

B4X:
Sub insertar_(mapa_mensaje As Map)

    If mapa_mensaje.Get ("aer") = Null Then
    Else
    If mapa_mensaje.Get("ae") = value_item.Get("ae") Then
    If mapa_mensaje.Get("usuario_Uid") = value_item.Get("usuario_Uid") Then
      
    Else
  
  
    viajeros_cargados.Put(mapa_mensaje.Get("usuario_Uid"),mapa_mensaje.Get("usuario_Uid"))
              
        Log(mapa_mensaje.Get("usuario_Uid"))
        Log(value_item.Get("usuario_Uid"))
      
    Dim lao As StringFunctions
    Private p As Panel
    p.Initialize("p")
    p.Visible = False
    p.Color = Colors.Green

    Activity.AddView(p,2%X,0,96%x,5%Y)
    p.LoadLayout("insertar_v")
    p.RemoveView
    'Seteo el tamaño
  
    Label_titular.Text = mapa_mensaje.Get("titular")
 
          
    Panel_viajero.SetLayout(3%x,1%y,94%X,12%y)
    ImageView_foto.SetLayoutAnimated(4%x,5%x,1%y,20%x,11.5%y)
    Label_titular.SetLayout(28%x,2%y,65%x,4%y)
    label_conectar.Visible = True
    label_conectar.SetLayout(63%x,4%y,30%x,5%y)
    label_conectar.TextColor = Colors.White
    Label_aceptar.Visible = False
  
  
    Label_titular.TextSize = 14
 
                label_conectar.TextSize = 10
 
                descargar_imagen(mapa_mensaje)             
              
  
    p.Height = Label_titular.Height + 8dip
  
      
    p.Color = Colors.White
    'p.Padding = Array As Int (15dip, 15dip, 15dip, 15dip)
  
            p.Visible = True


            Dim mapa_viajero As Map
            mapa_viajero.Initialize
            mapa_viajero = mapa_mensaje
            mapa_viajero.Put("estado","conectar")
              
                              
            If mapa_mensaje.Get("buscar") = "busqueda" Then
          
            CustomListView_busqueda.Add(p,mapa_viajero)
          
          
            End If
          
          
      
  If CustomListView_viajeros.Size >0  Then
    
    CustomListView_viajeros.Refresh
    CustomListView_viajeros.AsView
    CustomListView_viajeros.AsView.Color = Colors.White
    CustomListView_viajeros.JumpToItem(CustomListView_viajeros.Size-1)
    CustomListView_viajeros.AsView.SendToBack
 
            End If
        End If
    End If
End If
End Sub

Sub descargar_imagen(mapa_mensaje As Map)
    Try
        If Starter.auth.CurrentUser.IsInitialized Then
  
  
            Starter.storage.DownloadFile($"/public/${mapa_mensaje.Get("usuario_Uid")}/foto"$ , File.DirInternal,mapa_mensaje.Get("usuario_Uid"))
                  
            wait for Storage_DownloadCompleted (ServerPath As String, Success As Boolean)
                
            If Success Then
                Dim img As B4XBitmap = xui.LoadBitmap(File.DirInternal,mapa_mensaje.Get("usuario_Uid"))
                ImageView_foto.SetBitmap(CreateRoundBitmap(img, ImageView_foto.Width/1.3))
      
            Else
      
                Log(LastException)
      
                If Starter.auth.CurrentUser.PhotoUrl = "" Then
          
                Else
                  
                    DownloadImage(mapa_mensaje.Get("foto_url"),ImageView_foto, mapa_mensaje.Get("usuario_Uid"))
                  
                    'ver si puse bien esto
                End If
      
            End If
        End If
    Catch
        Log("no pudo descargar imagen")
      
    End Try

End Sub



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 / 2 - 2dip, xui.Color_White, False, 5dip) '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 B4XView,usuario 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, usuario, False)
    File.Copy2(j.GetInputStream, out)
    out.Close
        Dim img As B4XBitmap = xui.LoadBitmap(File.DirInternal, usuario)
      
        iv.SetBitmap(CreateRoundBitmap(img,15%x))
      
    End If
    j.Release
  
End Sub
 
Top