Android Question Uploading many files on FTP.

red30

Well-Known Member
Licensed User
Longtime User
I upload many files on FTP and do it that way:
B4X:
Sub UpLoad
ToastMessageShow("Upload, wait...",False)
UpLo=File.ListFiles(File.DirInternal&"/test")
kolupload=UpLo.Size-1
If UpLo.Size<>0 Then
FTP.UploadFile(File.DirInternal&"/test",UpLo.Get(kolupload),False,"/test/"&UpLo.Get(kolupload))
Else
ToastMessageShow("Nothing upload...",True)
End If
End Sub
Sub FTP_UploadCompleted (ServerPath As String, Success As Boolean)
Log(ServerPath & ", Success=" & Success)
If Success = False Then
Log(LastException.Message)
ToastMessageShow(LastException.Message,False)
Else
If kolupload=0 Then
ToastMessageShow("All files uploaded",False)
Else
kolupload=kolupload-1
FTP.UploadFile(File.DirInternal&"/test",UpLo.Get(kolupload),False,"/test/"&UpLo.Get(kolupload))
End If
End If
End Sub
If uploading is long (the internet is slow or there are many files), then the phone goes to sleep mode and the uploading stops. How can I make it not to stop after going to sleeping mode or blocking? Or to make the phone not to block itself till the uploading is finished?
 

DonManfred

Expert
Licensed User
Longtime User
Use a foreground service and do the upload here.
 
Upvote 0

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
This code can help you

B4X:
        Try
            ProgressDialogShow2("Subiendo fotos al servidor...",False)
            Dim fileFoto As List
            Dim reemplazo As String
            fileFoto.Initialize
            fileFoto = File.ListFiles(FotoFileDir)
            FTPinicia
            ServidorFTP.SendCommand("MKD", "/"& Servicio.usuario & "/Fotos")'Crea una carpeta con el nombre del usuario
            If fileFoto.Size = 0 Then
                ToastMessageShow("No se ha encontrado imagenes ", True)
                ProgressDialogHide
            Else
                For i = 0 To fileFoto.Size - 1
                    reemplazo = fileFoto.Get(i)
                    reemplazo = reemplazo.Replace(":","")
                    ServidorFTP.UploadFile(FotoFileDir,fileFoto.Get(i), False, "/"& Servicio.usuario & "/Fotos/" & reemplazo)
                    Wait For FTP_UploadCompleted (ServerPath As String, Success As Boolean)
                    If Success Then
                        Log("Archivo " & reemplazo & "Se subió satisfactoriamente" )
                        File.Delete(FotoFileDir,fileFoto.Get(i))
                    Else
                        ToastMessageShow("No subió "& reemplazo & ", favor intentar de nuevo.",True)
                        Log("No subió "& reemplazo)
                    End If
                    Sleep(0)
                Next
                LoadImagen
            End If
            ProgressDialogHide
        Catch
            ProgressDialogHide
            Log(LastException)
        End Try
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
This code can help you

B4X:
        Try
            ProgressDialogShow2("Subiendo fotos al servidor...",False)
            Dim fileFoto As List
            Dim reemplazo As String
            fileFoto.Initialize
            fileFoto = File.ListFiles(FotoFileDir)
            FTPinicia
            ServidorFTP.SendCommand("MKD", "/"& Servicio.usuario & "/Fotos")'Crea una carpeta con el nombre del usuario
            If fileFoto.Size = 0 Then
                ToastMessageShow("No se ha encontrado imagenes ", True)
                ProgressDialogHide
            Else
                For i = 0 To fileFoto.Size - 1
                    reemplazo = fileFoto.Get(i)
                    reemplazo = reemplazo.Replace(":","")
                    ServidorFTP.UploadFile(FotoFileDir,fileFoto.Get(i), False, "/"& Servicio.usuario & "/Fotos/" & reemplazo)
                    Wait For FTP_UploadCompleted (ServerPath As String, Success As Boolean)
                    If Success Then
                        Log("Archivo " & reemplazo & "Se subió satisfactoriamente" )
                        File.Delete(FotoFileDir,fileFoto.Get(i))
                    Else
                        ToastMessageShow("No subió "& reemplazo & ", favor intentar de nuevo.",True)
                        Log("No subió "& reemplazo)
                    End If
                    Sleep(0)
                Next
                LoadImagen
            End If
            ProgressDialogHide
        Catch
            ProgressDialogHide
            Log(LastException)
        End Try
How it works? Running the program does not stop uploading files if I click the lock button?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
How can I see the value of the variable from the activity in the service module? Can I build FTP in the main activity, download a file from FTP, and then go to the server module and upload the files I need?
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
You can use KeepAlive to keep the phone from closing the activity. It works well. I recommend to also watch the battery charge state to force the activity to shut down if the battery is too low.
Put this in Process_Globals:
B4X:
Public pws As PhoneWakeState
This requires the Phone library.
Add this in Activity_Resume:
B4X:
pws.KeepAlive( True )
Create a couple new subs (adjust the 50 to whatever you feel comfortable with):
B4X:
Sub PhoneEvent_BatteryChanged( Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent )
    PluggedStatus = Plugged   
    ChargeLevel = Level
    If PluggedStatus = False And ChargeLevel < 50 Then
        pws.KeepAlive( False )
    Else
        pws.KeepAlive( True )
    End If
End Sub ' PhoneEvent_BatteryChanged()

Sub Activity_Pause( UserClosed As Boolean )
    If UserClosed = True Then pws.ReleaseKeepAlive 
End Sub ' Activity_Pause()

and do not forget to do that before voluntarily closing the Activity:
B4X:
pws.ReleaseKeepAlive
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
How can I see the value of the variable from the activity in the service module?
Start a sub in the service from the Activity giving all Infos you need in the Service.
In Activity, for ex in Main

B4X:
    Dim meta As Map
    meta.Initialize
    meta.Put("Value1",1234)
    meta.Put("AnotherValue","0815")
    CallSub3(Starter,"dothings",Me,meta) ' starter is always running. no need for callsubdelayed

in starter

B4X:
Sub dothings(callback As Object, meta As Map)
    Log($"dothings(${meta})"$)   
    ' do the ftp upload here
    ' wait for upload finish
    ' inform the caller about the finished upload
    Dim result As Map
    result.Initialize
    result.Put("Status","Upload finished")
    result.Put("Value1",meta.Get("Value1"))
    result.Put("AnotherValue",meta.Get("Value2"))
    CallSubDelayed2(callback,"UploadResult",result) ' send the result back to caller activity. Note to use callsubdelayed here
End Sub

in activity (same activity as above)
B4X:
Sub UploadResult(info As Map)
    Log($"Main.UploadResult(${info}"$)
End Sub

LOG
dothings({Value1=1234, AnotherValue=0815}) ' starter
Main.UploadResult({Status=Upload finished, Value1=1234, AnotherValue=null} ' ativity which called the sub in starter
 
Upvote 0
Top