Spanish Descarga imagenes rapida

Jorge Sanz

Member
Licensed User
Hola,

Tengo el siguiente código para realizar la descarga de imagenes que tengo en mi ftp y funciona perfectamente. Pero tengo alguna duda:
- Hay bastantes imagenes en el ftp y no sé si estoy utilizando la mejor solución para descargarlas ya que cuando utilizo algunas apps (mil anuncios,twitter,etc), noto que realizan la carga inmediatamente.

- Es mejor descargarlas todas al inicio o ir descargandolas poco a poco (cuando llegue al final del listado)



B4X:
Sub Activity_Create(FirstTime As Boolean)
.
.
.
.
    
    ProgressDialogShow2("Cargando, espere...", False)
    n_item =10
    contitem=n_item   
    'min_item = 1
    min_item = 0
    ultimo_item = 3
    Dim l As List
    l.Initialize
    l.Add(CustomList.AsView)
    STR.SwipeableChilden = l
    links.Initialize
    todownload.Initialize
   
    
    ExecuteRemoteQuery("Select * From Articulos Order By CodigoArticulo DESC","ARTICULOS")
        

    
End Sub



Sub JobDone(Job As HttpJob)
    
    If Job.Success Then
    
        Dim res As String
        res = Job.GetString
        Log("Response from server: " & res)
        Dim parser As JSONParser
        parser.Initialize(res)
        Select Job.JobName
            Case "Downloadmimarket"
                Dim p As String = Job.Tag
                Dim OutStream As OutputStream
                Log("DownloadReady: "&Job.Tag)
                OutStream = File.OpenOutput(DirmTMPMarket , GetFilename(p), False)
                
                File.Copy2(Job.GetInputStream,OutStream) ' save the file
                OutStream.Close
                Log(GetFilename(p)&" is written to "&File.DirDefaultExternal)
                
                ' Remove the first job from queue
                If todownload.Size > 0 Then
                    todownload.RemoveAt(0)
                End If

                ' Check if there are more files to download. If yes. Download them
                If todownload.Size > 0 Then
                    Dim furl As String = todownload.Get(0)
                    Log("Download: "&furl)
                    Dim j As HttpJob
                    j.Initialize("Downloadmimarket",Me)
                    j.Tag = furl
                    j.Download(furl)
                Else
                 
                    CargaLista2
                End If
                
         
            
            Case "ARTICULOS"
                ProgressDialogShow ("Cargando ...")
                Dim res As String
                Dim rutadescarga,img As String
                res = Job.GetString
                Log("Response from server: " & res)
                Dim parser As JSONParser
                parser.Initialize(res)
                ListArticulos = parser.NextArray 'returns a list with maps
                result.Initialize
                'Dim imagefolder2 As String
                
                totalarticulos=ListArticulos.Size
                For i = 0 To ListArticulos.Size - 1
                    Dim regarticulos As articulos
                    m = ListArticulos.Get(i)
                    regarticulos.CodigoArticulo=m.Get("CodigoArticulo")
                    regarticulos.Categoria=m.Get("Categoria")
                    regarticulos.Titulo=m.Get("Titulo")
                    regarticulos.Descripcion=m.Get("Descripcion")
                    regarticulos.Precio=m.Get("Precio")
                    regarticulos.contacto=m.Get("Contacto")
                    regarticulos.Tipo=m.Get("Tipo")
                   
                    If  File.Exists(DirmTMPMarket, m.Get("CodigoArticulo") & ".jpg")=False     Then
                        todownload.Add("http://xxxx.com/" & m.Get("CodigoArticulo") & ".jpg")
                    End If
                    
                    rutamovil= DirmTMPMarket
                    
                    links.Add(regarticulos)
                Next
                
                If todownload.Size > 0 Then
                    Dim furl As String = todownload.Get(0)
                    Log("Download: "&furl)
                    Dim j As HttpJob
                    j.Initialize("Downloadmimarket",Me)
                    j.Tag = furl
                    j.Download(furl)
                End If
                CargaLista2
                'refresh
                'ProgressDialogHide
            
        End Select
    Else
        'ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    ProgressDialogHide
    Job.Release
End Sub

Sub CargaLista2
    Dim totalitem As Int
    Sleep(200)
    'For i = min_item To n_item
    If links.Size < n_item Then
        totalitem=links.Size
    Else
        totalitem=n_item
    
    End If
    For i = min_item To totalitem-1
        regarticulos=links.Get(i)
        'CustomList.Add(CreateListItem($"Item #${i}"$, CustomList.AsView.Width, 270dip), 282dip, $"Item #${i}"$)
        If GetDeviceLayoutValues.ApproximateScreenSize > 9.1 Then
            CustomList.Add(CreateListItem(regarticulos.Titulo, CustomList.AsView.Width, 400dip,regarticulos), 450dip, regarticulos.codigoarticulo)
        Else
            'CustomList.Add(CreateListItem(regarticulos.Titulo, CustomList.AsView.Width, 270dip,regarticulos), 320dip, regarticulos.codigoarticulo)
            CustomList.Add(CreateListItem(regarticulos.Titulo, CustomList.AsView.Width, 270dip,regarticulos), 320dip, regarticulos.codigoarticulo)
        End If
        
    Next
  
    ProgressDialogHide
    min_item = n_item
End Sub

Sub CreateListItem(Text As String, Width As Int, Height As Int,regarticulo As articulos) As Panel
    If regarticulo.codigoarticulo<>"" Then
        Dim p As Panel
        p.Initialize("")
        'we need to add the panel to a parent to set its dimensions. It will be removed after the layout is loaded.
        Activity.AddView(p, 0, 0, Width, Height)
        p.LoadLayout("LayoutCellItem")
        CardView.LoadLayout("LayoutMscard")
        Label3.Text=Text
        precio.Text=regarticulo.Precio & " €"
        'telefono.Text=regarticulo.telefono
        titmail.Text=regarticulo.contacto
        If File.Exists(DirmTMPMarket,regarticulo.codigoarticulo & ".jpg") Then
            ImageView1.Bitmap=LoadBitmap(DirmTMPMarket,regarticulo.codigoarticulo & ".jpg")
        Else
            ImageView1.Bitmap=LoadBitmap(File.DirAssets,"nofoto.jpg")
        End If
        tipoanuncio.text=regarticulo.Tipo
    
        RV.Initialize(LabelVermas, Colors.LightGray, 200, True)
        p.RemoveView
        'label1 and button1 will point to the last added views.
        Return p
    End If
End Sub

Sub CustomList_ReachEnd
    Log("reach end")
    ultimo_item = 2
    'CustomList.AddTextItem("New item!!!", "")
    STR.Refreshing = True
    STR_Refresh
End Sub



Sub STR_Refresh
    'Msgbox ("refresh","a")
    Log("Refresh started")
    'CustomList.Clear
    'Start the timer. Normally you will start your asynchronous job here
    'links.Initialize
    
    
   
    
    tm.Initialize("Timer", 2000)
    tm.Enabled = True
   
    'Disable the STR object so we can not start a new refresh process
    STR.Enabled = True
    cargando=False
End Sub

'The timer tick simulates the end of the refreshing process
Sub Timer_Tick
    Log("Refresh stopped")
    
    'Stop Timer and refresh the listview data
    tm.Enabled = False
    If ultimo_item == 2 Then
        'n_item = n_item + 10
        n_item = n_item + 10
        contitem=n_item
        min_item = min_item + 1
        Log("Item a Agregar: " & n_item)
    Else
        CustomList.Clear
        min_item = 0
    End If
    ultimo_item = 1
    'CargaLista
    CargaLista2

    'Stop the spinning disc and enable the STR object again.
    STR.Refreshing = False
    STR.Enabled = True
End Sub
 

Descartex

Well-Known Member
Licensed User
Longtime User
Muy buenas.
Lo primero, es optimizar el tamaño para que ocupen lo mínimo indispensable, con ello, la carga será evidentemente más rapida.
Segundo, bajo mi punto de vista, es mejor ir mostrando según se descargan, guardar una copia en el dispositivo y continuar la tarea.

Un saludo.
 
Top