Android Question Strange behavior in SaveAS (Erel example)

Gerardo Tenreiro

Active Member
Licensed User
Hello,
In a fairly large application I have three calls to SaveAs, as per Erel's example.
Two of the calls work fine, but one of them which is in a code module does not give an error but the file is created empty, without the data in the SaveAs, however the file in the android folder is correct and with all the data inside.

The files are CSV files of a size of approximately 50kB each.

It is also curious that the lines after the
"Wait For (B4XPages.MainPage.SaveAs(File.OpenInput(Path,File), "application/octet-stream", File)) Complete (Success As Boolean)"
do not execute.

I am quite confused with this behavior so any help is of great value.

I leave you part of the code, where the call that does not work is to see if you can help me.

B4X:
Sub BT_Exportar_Click
    Dim C                As Int        = 0
    Dim D                As Int         = 0
    Dim Ayuda             As String    = ""
    Dim Fichero         As String     = ""
    Dim Ruta            As String     = ""
    Dim Ay                As String     = ""
    
    ' Impide Pulsar el Boton Antes de Terminas
    Bt_Exportar.Enabled = False
    ' Nombre del Fichero.
    Fichero = "ICDoor_Exp01_" & NumberFormat(DateTime.GetDayOfMonth(DateTime.Now) ,2,0)
    Fichero = Fichero & NumberFormat(DateTime.GetMonth(DateTime.Now),2,0)
    Fichero = Fichero & NumberFormat(DateTime.GETYEAR(DateTime.Now)-2000,2,0)
    Fichero = Fichero & "_"
    Fichero = Fichero &  NumberFormat(DateTime.GetHour(DateTime.Now),2,0)
    Fichero = Fichero &  NumberFormat(DateTime.GetMinute(DateTime.Now),2,0)
    Fichero = Fichero &  NumberFormat(DateTime.GetSecond(DateTime.Now),2,0)
    Fichero = Fichero & ".CSV"
    ' Ruta del Fichero
    Ruta = File.DirDefaultExternal                        ' File.DirInternal o  File.DirDefaultExternal
    
    Log("Inicia Exportacion ")
    Log("Fichero =" & Fichero)
    Log("Directorio=" & Ruta)
    
    LB_Exportando.Visible = True
    B_Exporta.Visible = True
    
    ToastMessageShow("Exportando Datos ...",True)
    ' Cabecera del Fichero CVS
    Ayuda = "Gráfica de IC-DOOR" & Chr(10) & Chr(13)
    If B4XPages.MainPage.A_T_Logger = 0 Then
        Ayuda = Ayuda & "Muestreo por Cambio de Valor. [BY G.T.V]" & Chr(10) & Chr(13)
    Else
        Ayuda = Ayuda & "Intervalo entre muestras"
        Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.A_T_Logger ,3,1)
        Ayuda = Ayuda &     " ms. [BY G.T.V]" & Chr(10) & Chr(13)
    End If
    ' Cabecera del Fichero
    Ayuda = Ayuda & "Registro;"
    Ayuda = Ayuda & "Posición Encoder;"
    Ayuda = Ayuda & "Posición Encoder Filtrada;"
    Ayuda = Ayuda & "Posición Puerta;"
    Ayuda = Ayuda & "Consigna Velocidad;"
    Ayuda = Ayuda & "Intensidad Motor;"
    Ayuda = Ayuda & "Calidad Total;"
    Ayuda = Ayuda & "Calidad Parcial;"
    Ayuda = Ayuda & "Secuencia;"
    Ayuda = Ayuda & "Modo Maniobra;"
    Ayuda = Ayuda & "Puerta Abierta;"
    Ayuda = Ayuda & "Puerta Cerrada;"
    Ayuda = Ayuda & "Puerta Abriendo;"
    Ayuda = Ayuda & "Puerta Cerrando;"
    Ayuda = Ayuda & "Puerta Parada;"
    Ayuda = Ayuda & "Orden Freno;"
    Ayuda = Ayuda & "Orden Abrir;"
    Ayuda = Ayuda & "Orden Cerrar;"
    Ayuda = Ayuda & "Foto Célula;"
    Ayuda = Ayuda & "PRG Pide Abrir;"
    Ayuda = Ayuda & "PRG Pide Cerrar;"
    Ayuda = Ayuda & "Alguna Orden M;"
    Ayuda = Ayuda & "Contactor A;"
    Ayuda = Ayuda & "Contactor C;"
    Ayuda = Ayuda & "Emergencia Ok;"
    Ayuda = Ayuda & "E00;"
    Ayuda = Ayuda & "E01;"
    Ayuda = Ayuda & "PLC Run;"
    Ayuda = Ayuda & "PE Encoder;"
    Ayuda = Ayuda & "Fallo Test FotoCelula;"
    Ayuda = Ayuda & "Fallo Comunica Encoder;"
    Ayuda = Ayuda & "Encoder No se Mueve;"
    Ayuda = Ayuda & "PE_Ext;"
    Ayuda = Ayuda & "Fallo VF;"
    Ayuda = Ayuda & "FC.A;"
    Ayuda = Ayuda & "FC.C;"
    Ayuda = Ayuda & "DB.1;"
    Ayuda = Ayuda & "DB.2;"
    Ayuda = Ayuda & "P.Radio 1;"
    Ayuda = Ayuda & "P.Radio 2;"
    Ayuda = Ayuda & "Ent.Bloqueo A;"
    Ayuda = Ayuda & "Fallo Vf"
    Ayuda = Ayuda & Chr(10) & Chr(13)
    
    D = 0                                ' Inicializa   
    For C = 0 To Posicion
        LB_Exportando.Text = "Exp." & C & " de " & Posicion
        B_Exporta.Progress = (C*100) / Posicion

        D = D + 1
        Ayuda = Ayuda & C & ";"                                                            ' Registro
        Ayuda = Ayuda & NumberFormat2(Main.Logger(C).P_Encoder,  5,0,0,False)    & ";"    ' Posicion Encoder
        Ayuda = Ayuda & NumberFormat2(Main.Logger(C).P_Encoder_F,5,0,0,False)    & ";"    ' Posicion Encoder filtrada
        Ay    =         NumberFormat2(Main.Logger(C).P_Puerta,   3,2,2,False)    & ";"    ' Posicion Puerta
        If Ay.Contains(".") Then
            Ay = Ay.Replace(".",",")    ' Cambia "." por ","
        End If
        Ayuda = Ayuda & Ay
        Ay       =         NumberFormat2(Main.Logger(C).C_Velocidad,3,2,2,False)    & ";"    ' Consigna Velocidad
        If Ay.Contains(".") Then
            Ay = Ay.Replace(".",",")    ' Cambia "." por ","
        End If
        Ayuda = Ayuda & Ay
        Ay       =         NumberFormat2(Main.Logger(C).Intensidad_M,2,3,3,False)    & ";"    ' Intensidad Motor
        If Ay.Contains(".") Then
            Ay = Ay.Replace(".",",")    ' Cambia "." por ","
        End If
        Ayuda = Ayuda & Ay
        Ayuda = Ayuda & NumberFormat2(Main.Logger(C).CT_Encoder, 4,0,0,False)    & ";"    ' Calidad Total encoder
        Ayuda = Ayuda & NumberFormat2(Main.Logger(C).CP_Encoder, 4,0,0,False)    & ";"    ' Calidad Parcial encoder
        Ayuda = Ayuda & NumberFormat2(Main.Logger(C).Secuencia, 3,0,0,False)    & ";"    ' Numero de Secuencia
        Ayuda = Ayuda & NumberFormat2(Main.Logger(C).Modo_M, 3,0,0,False)        & ";"    ' Modo Maniobra
        
        
        ' Digitales 1
        If Main.Logger(C).P_Abierta     = True Then                                     ' Puerta Abierta             1.0
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).P_Cerrada         = True Then                                    ' Puerta Cerrada             1.1
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).P_Abriendo     = True Then                                    ' Puerta Abriendo             1.2
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).P_Cerrando     = True Then                                        ' Puerta Cerrando             1.3
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).P_Parada         = True Then                                        ' Puerta Parada                1.4
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).O_Freno        = True Then                                        ' Orden Freno Puerta        1.5
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).O_Abrir          = True Then                                        ' Orden Abrir                1.6
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).O_Cerrar         = True Then                                     ' Orden Cerrar                1.7
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        ' Digitales 2
        If Main.Logger(C).Foto_Celula    = True Then                                     ' Estado Foto Celula        2.0
            Ayuda = Ayuda & "1;"
        Else
            Ayuda = Ayuda & "0;"
        End If
        If Main.Logger(C).PRG_Pide_A     = True Then                                     ' PRG Pide Abrir            2.1
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).PRG_Pide_C     = True Then                                     ' PRG Pide Cerrar            2.2
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).Alguna_Orden_M     = True Then                                 ' Alguna Orden Movimiento    2.3
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).Contactor_A     = True Then                                     ' Contactor Abrir            2.4
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).Contactor_C     = True Then                                     ' Contactor Cerrar            2.5
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).Emergencia_OK = True Then                                     ' Parada de Emergencia        2.6
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E00 = True Then                                                 ' E00                        2.7
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        ' Digital 3
        If Main.Logger(C).E01 = True Then                                                 ' E01                        3.0
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).PLC_Run = True Then                                             ' PLC en RUN                3.1
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        
        ' Digitales 3
        If Main.Logger(C).Pe_Encoder     = True Then                                     ' Parada Emergencia Encoder    3.2
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).Fallo_T_FT     = True Then                                     ' Fallo Test de Fotocelula    3.3
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).F_C_E     = True Then                                         ' Fallo Comunicacion Encoder    3.4
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).Memo_E_NM     = True Then                                     ' Fallo  Encoder No se Mueve    3.5
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).PE_Ext     = True Then                                         ' Pulsadores PE Parametrizables.3.6
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).VF_Fallo = True Then                                             ' Fallo del VF                    3.7
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        ' Digital 4
        If Main.Logger(C).E11 = True Then                                                 ' Final de Carrera Abierta        4.0
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E12 = True Then                                                 ' Final de Carrera Cerrada        4.1
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E13 = True Then                                                 ' Detector de Banda 1            4.2
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E14 = True Then                                                 ' Detector de Banda 2            4.3
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E15 = True Then                                                 ' Pulsador de Radio 1            4.4
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E16 = True Then                                                 ' Pulsador de Radio 2            4.5
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E06 = True Then                                                 ' Entrada Bloqueo de Apertura    4.6
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E10 = True Then                                                 ' Entrada Fallo del VF            4.7
            Ayuda = Ayuda & "1"
        Else
            Ayuda = Ayuda & "0"
        End If
        
        Ayuda = Ayuda & Chr(10) & Chr(13)
        Sleep(0)
    Next
    
    ' Incluir Datos de Estadistica
    Ayuda = Ayuda & Chr(10) & Chr(13)
    Ayuda = Ayuda & "ESTADISTICA" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Horas de Uso =;;;;;;" & NumberFormat(B4XPages.MainPage.ES_Horas_Uso,8,0) & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Arranques =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_N_Arranques,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Tecla ENTER =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_P_Tecla_Enter,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Tecla Mas =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_P_Tecla_Mas,8,0) & ";"  & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Tecla Menos =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_P_Tecla_Menos,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Abrir =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Abrir,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Cerrar =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Cerrar,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt PE =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_PE,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Fallos VF =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Fallos_VF,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Orden A VF =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_O_A_VF,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Orden C VF =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_O_C_VF,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Fallos VF =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Fallos_VF,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Rele 1 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_RELE_1,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Rele 2 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_RELE_2,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Rele 3 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_RELE_3,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Rele 4 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_RELE_4,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Reserva 1 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Reserva_1,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Reserva 2 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Reserva_2,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Reserva 3 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Reserva_3,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Freno =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_Freno,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Fallos Encoder =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Fallos_C_Encoder,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt PE Encoder =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Fallos_seg_Encoder,8,0) & Chr(10) & Chr(13)
    
    ' Guarda en Fichero Android\data\
    File.WriteString(Ruta, Fichero, Ayuda)
    
    
    ' Presenta Informacion
    Log("Final Exportacion")
    Log("Ruta =" & Ruta )
    Log("Fichero =" & Fichero)
    Log("Largo Fichero  =" & File.Size(Ruta,Fichero))
    
    ' Activa Boton Exportar
    Bt_Exportar.Enabled     = True   
    LB_Exportando.Visible     = False
    B_Exporta.Visible         = False
    
    Log("Abre Ventana para Guardar el Fichero")
    
    Wait For (B4XPages.MainPage.SaveAs(File.OpenInput(Ruta,Fichero), "application/octet-stream", Fichero)) Complete (Success As Boolean)
    If Success = True Then
        Control.LogMessage("* ","Fichero Guardado Correctamente")
        ToastMessageShow("Fichero Creado correctamente",True)
    Else
        Control.LogMessage("* ","Error al Guardar el Fichero")
        ToastMessageShow("Error al crear el fichero de parametros",True)
    End If

End Sub

Thank you very much
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
Hi,

Thanks for the help

I created a Sub like this:
' ' Public Sub Guarda_Exporta_Grafica(Ruta As String,Fichero As String) Wait For (B4XPages.MainPage.SaveAs(File.OpenInput(Ruta, Fichero), "application/octet-stream", Fichero)) Complete (Success As Boolean) If Success = True Then ToastMessageShow("Fichero Guardado Correctamente",True) LogMessage("* ","Fichero Guardado Correctamente") Else ToastMessageShow("Error al guardar el fichero",True) LogMessage("* ","Error al Guardar el Fichero") End If End Sub:
'
'
Public Sub Guarda_Exporta_Grafica(Ruta As String,Fichero As String)
   
    Wait For (B4XPages.MainPage.SaveAs(File.OpenInput(Ruta, Fichero), "application/octet-stream", Fichero)) Complete (Success As Boolean)
    If Success = True Then
        ToastMessageShow("Fichero Guardado Correctamente",True)
        LogMessage("* ","Fichero Guardado Correctamente")
    Else
        ToastMessageShow("Error al guardar el fichero",True)
        LogMessage("* ","Error al Guardar el Fichero")
    End If
End Sub

I call it that
B4X:
    Log("Final Exportacion")
    Log("Ruta =" & Ruta )
    Log("Fichero =" & Fichero)
    Log("Largo Fichero  =" & File.Size(Ruta,Fichero))
   
    ' Activa Boton Exportar
    Bt_Exportar.Enabled     = True  
    LB_Exportando.Visible     = False
    B_Exporta.Visible         = False
   
    Log("Abre Ventana para Guardar el Fichero")

    Control.Guarda_Exporta_Grafica(Ruta,Fichero)
   
'    Wait For (B4XPages.MainPage.SaveAs(File.OpenInput(Ruta,Fichero), "application/octet-stream", Fichero)) Complete (Success As Boolean)
'    If Success = True Then
'        Control.LogMessage("* ","Fichero Guardado Correctamente")
'        ToastMessageShow("Fichero Creado correctamente",True)
'    Else
'        Control.LogMessage("* ","Error al Guardar el Fichero")
'        ToastMessageShow("Error al crear el fichero de parametros",True)
'    End If



When calling it from the cogigo module this error is generated:

Ruta =/storage/emulated/0/Android/data/gtv.ic.door/files
Fichero =ICDoor_Exp01_110924_103053.CSV
Largo Fichero =5860
Abre Ventana para Guardar el Fichero
Error occurred on line: 655 (Grafica)
java.lang.RuntimeException: Class instance was not initialized (chatpage)
at anywheresoftware.b4a.debug.Debug.shouldDelegate(Debug.java:242)
at gtv.ic.door.chatpage._guarda_exporta_grafica(chatpage.java:2921)
at gtv.ic.door.grafica$ResumableSub_BT_Exportar_Click.resume(grafica.java:2418)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1748)
at android.os.Handler.handleCallback(Handler.java:899)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:7888)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:512)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:998)
** Activity (main) Resume **

What am I doing wrong?
Any suggestions?
Thank you very much
 
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
Hello,
I continue to have the same problem but now I know that sometimes it works correctly.
When you run the code, the file is created in the indicated folder, the size is correct and the file is correct.
When you call "SAVEAS" everything seems to work, but the file is created empty although other times it is created correctly.

Any idea where to continue looking for this error?
Thank you very much
 
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
Good morning
The example works correctly

In my case I am calling from a code module to SaveAs, the strange thing is that I have two other calls that are made from a class module and they work.
I also observe that in the call that I make from the code module the response is not executed, let me explain, I have no response in the log of what happened despite having the code written correctly.

The program in the code module call is this:

B4X:
'
' Exporta Datos a Fichero
Sub BT_Exportar_Click
    Dim C                As Int        = 0
    Dim D                As Int         = 0
    Dim Ayuda             As String    = ""
    Dim Fichero         As String     = ""
    Dim Ruta            As String     = ""
    Dim Ay                As String     = ""
    
    ' Impide Pulsar el Boton Antes de Terminas
    'Grafica.BT_Exportar.Enabled = False
    ' Nombre del Fichero.
    Fichero = "ICDoor_Exp01_" & NumberFormat(DateTime.GetDayOfMonth(DateTime.Now) ,2,0)
    Fichero = Fichero & NumberFormat(DateTime.GetMonth(DateTime.Now),2,0)
    Fichero = Fichero & NumberFormat(DateTime.GETYEAR(DateTime.Now)-2000,2,0)
    Fichero = Fichero & "_"
    Fichero = Fichero &  NumberFormat(DateTime.GetHour(DateTime.Now),2,0)
    Fichero = Fichero &  NumberFormat(DateTime.GetMinute(DateTime.Now),2,0)
    Fichero = Fichero &  NumberFormat(DateTime.GetSecond(DateTime.Now),2,0)
    Fichero = Fichero & ".CSV"
    ' Ruta del Fichero
    Ruta = File.DirDefaultExternal                        ' File.DirInternal o  File.DirDefaultExternal
    
    Log("Inicia Exportacion ")
    Log("Fichero =" & Fichero)
    Log("Directorio=" & Ruta)
    
    LB_Exportando.Visible = True
    B_Exporta.Visible = True
    
    ToastMessageShow("Exportando Datos ...",True)
    ' Cabecera del Fichero CVS
    Ayuda = "Gráfica de IC-DOOR" & Chr(10) & Chr(13)
    If B4XPages.MainPage.A_T_Logger = 0 Then
        Ayuda = Ayuda & "Muestreo por Cambio de Valor. [BY G.T.V]" & Chr(10) & Chr(13)
    Else
        Ayuda = Ayuda & "Intervalo entre muestras"
        Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.A_T_Logger ,3,1)
        Ayuda = Ayuda &     " ms. [BY G.T.V]" & Chr(10) & Chr(13)
    End If
    ' Cabecera del Fichero
    Ayuda = Ayuda & "Registro;"
    Ayuda = Ayuda & "Posición Encoder;"
    Ayuda = Ayuda & "Posición Encoder Filtrada;"
    Ayuda = Ayuda & "Posición Puerta;"
    Ayuda = Ayuda & "Consigna Velocidad;"
    Ayuda = Ayuda & "Intensidad Motor;"
    Ayuda = Ayuda & "Calidad Total;"
    Ayuda = Ayuda & "Calidad Parcial;"
    Ayuda = Ayuda & "Secuencia;"
    Ayuda = Ayuda & "Modo Maniobra;"
    Ayuda = Ayuda & "Puerta Abierta;"
    Ayuda = Ayuda & "Puerta Cerrada;"
    Ayuda = Ayuda & "Puerta Abriendo;"
    Ayuda = Ayuda & "Puerta Cerrando;"
    Ayuda = Ayuda & "Puerta Parada;"
    Ayuda = Ayuda & "Orden Freno;"
    Ayuda = Ayuda & "Orden Abrir;"
    Ayuda = Ayuda & "Orden Cerrar;"
    Ayuda = Ayuda & "Foto Célula;"
    Ayuda = Ayuda & "PRG Pide Abrir;"
    Ayuda = Ayuda & "PRG Pide Cerrar;"
    Ayuda = Ayuda & "Alguna Orden M;"
    Ayuda = Ayuda & "Contactor A;"
    Ayuda = Ayuda & "Contactor C;"
    Ayuda = Ayuda & "Emergencia Ok;"
    Ayuda = Ayuda & "E00;"
    Ayuda = Ayuda & "E01;"
    Ayuda = Ayuda & "PLC Run;"
    Ayuda = Ayuda & "PE Encoder;"
    Ayuda = Ayuda & "Fallo Test FotoCelula;"
    Ayuda = Ayuda & "Fallo Comunica Encoder;"
    Ayuda = Ayuda & "Encoder No se Mueve;"
    Ayuda = Ayuda & "PE_Ext;"
    Ayuda = Ayuda & "Fallo VF;"
    Ayuda = Ayuda & "FC.A;"
    Ayuda = Ayuda & "FC.C;"
    Ayuda = Ayuda & "DB.1;"
    Ayuda = Ayuda & "DB.2;"
    Ayuda = Ayuda & "P.Radio 1;"
    Ayuda = Ayuda & "P.Radio 2;"
    Ayuda = Ayuda & "Ent.Bloqueo A;"
    Ayuda = Ayuda & "Fallo Vf"
    Ayuda = Ayuda & Chr(10) & Chr(13)
    
    D = 0                                ' Inicializa
    For C = 0 To Posicion
        LB_Exportando.Text = "Exp." & C & " de " & Posicion
        B_Exporta.Progress = (C*100) / Posicion

        D = D + 1
        Ayuda = Ayuda & C & ";"                                                            ' Registro
        Ayuda = Ayuda & NumberFormat2(Main.Logger(C).P_Encoder,  5,0,0,False)    & ";"    ' Posicion Encoder
        Ayuda = Ayuda & NumberFormat2(Main.Logger(C).P_Encoder_F,5,0,0,False)    & ";"    ' Posicion Encoder filtrada
        Ay    =         NumberFormat2(Main.Logger(C).P_Puerta,   3,2,2,False)    & ";"    ' Posicion Puerta
        If Ay.Contains(".") Then
            Ay = Ay.Replace(".",",")    ' Cambia "." por ","
        End If
        Ayuda = Ayuda & Ay
        Ay       =         NumberFormat2(Main.Logger(C).C_Velocidad,3,2,2,False)    & ";"    ' Consigna Velocidad
        If Ay.Contains(".") Then
            Ay = Ay.Replace(".",",")    ' Cambia "." por ","
        End If
        Ayuda = Ayuda & Ay
        Ay       =         NumberFormat2(Main.Logger(C).Intensidad_M,2,3,3,False)    & ";"    ' Intensidad Motor
        If Ay.Contains(".") Then
            Ay = Ay.Replace(".",",")    ' Cambia "." por ","
        End If
        Ayuda = Ayuda & Ay
        Ayuda = Ayuda & NumberFormat2(Main.Logger(C).CT_Encoder, 4,0,0,False)    & ";"    ' Calidad Total encoder
        Ayuda = Ayuda & NumberFormat2(Main.Logger(C).CP_Encoder, 4,0,0,False)    & ";"    ' Calidad Parcial encoder
        Ayuda = Ayuda & NumberFormat2(Main.Logger(C).Secuencia, 3,0,0,False)    & ";"    ' Numero de Secuencia
        Ayuda = Ayuda & NumberFormat2(Main.Logger(C).Modo_M, 3,0,0,False)        & ";"    ' Modo Maniobra
        
        
        ' Digitales 1
        If Main.Logger(C).P_Abierta     = True Then                                     ' Puerta Abierta             1.0
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).P_Cerrada         = True Then                                    ' Puerta Cerrada             1.1
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).P_Abriendo     = True Then                                    ' Puerta Abriendo             1.2
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).P_Cerrando     = True Then                                        ' Puerta Cerrando             1.3
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).P_Parada         = True Then                                        ' Puerta Parada                1.4
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).O_Freno        = True Then                                        ' Orden Freno Puerta        1.5
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).O_Abrir          = True Then                                        ' Orden Abrir                1.6
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).O_Cerrar         = True Then                                     ' Orden Cerrar                1.7
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        ' Digitales 2
        If Main.Logger(C).Foto_Celula    = True Then                                     ' Estado Foto Celula        2.0
            Ayuda = Ayuda & "1;"
        Else
            Ayuda = Ayuda & "0;"
        End If
        If Main.Logger(C).PRG_Pide_A     = True Then                                     ' PRG Pide Abrir            2.1
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).PRG_Pide_C     = True Then                                     ' PRG Pide Cerrar            2.2
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).Alguna_Orden_M     = True Then                                 ' Alguna Orden Movimiento    2.3
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).Contactor_A     = True Then                                     ' Contactor Abrir            2.4
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).Contactor_C     = True Then                                     ' Contactor Cerrar            2.5
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).Emergencia_OK = True Then                                     ' Parada de Emergencia        2.6
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E00 = True Then                                                 ' E00                        2.7
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        ' Digital 3
        If Main.Logger(C).E01 = True Then                                                 ' E01                        3.0
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).PLC_Run = True Then                                             ' PLC en RUN                3.1
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        
        ' Digitales 3
        If Main.Logger(C).Pe_Encoder     = True Then                                     ' Parada Emergencia Encoder    3.2
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).Fallo_T_FT     = True Then                                     ' Fallo Test de Fotocelula    3.3
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).F_C_E     = True Then                                         ' Fallo Comunicacion Encoder    3.4
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).Memo_E_NM     = True Then                                     ' Fallo  Encoder No se Mueve    3.5
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).PE_Ext     = True Then                                         ' Pulsadores PE Parametrizables.3.6
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).VF_Fallo = True Then                                             ' Fallo del VF                    3.7
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        ' Digital 4
        If Main.Logger(C).E11 = True Then                                                 ' Final de Carrera Abierta        4.0
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E12 = True Then                                                 ' Final de Carrera Cerrada        4.1
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E13 = True Then                                                 ' Detector de Banda 1            4.2
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E14 = True Then                                                 ' Detector de Banda 2            4.3
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E15 = True Then                                                 ' Pulsador de Radio 1            4.4
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E16 = True Then                                                 ' Pulsador de Radio 2            4.5
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E06 = True Then                                                 ' Entrada Bloqueo de Apertura    4.6
            Ayuda = Ayuda & "1" & ";"
        Else
            Ayuda = Ayuda & "0" & ";"
        End If
        If Main.Logger(C).E10 = True Then                                                 ' Entrada Fallo del VF            4.7
            Ayuda = Ayuda & "1"
        Else
            Ayuda = Ayuda & "0"
        End If
        
        Ayuda = Ayuda & Chr(10) & Chr(13)
        Sleep(0)
    Next
    
    ' Incluir Datos de Estadistica
    Ayuda = Ayuda & Chr(10) & Chr(13)
    Ayuda = Ayuda & "ESTADISTICA" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Horas de Uso =;;;;;;" & NumberFormat(B4XPages.MainPage.ES_Horas_Uso,8,0) & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Arranques =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_N_Arranques,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Tecla ENTER =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_P_Tecla_Enter,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Tecla Mas =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_P_Tecla_Mas,8,0) & ";"  & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Tecla Menos =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_P_Tecla_Menos,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Abrir =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Abrir,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Cerrar =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Cerrar,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt PE =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_PE,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Fallos VF =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Fallos_VF,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Orden A VF =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_O_A_VF,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Orden C VF =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_O_C_VF,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Fallos VF =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Fallos_VF,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Rele 1 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_RELE_1,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Rele 2 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_RELE_2,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Rele 3 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_RELE_3,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Rele 4 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_RELE_4,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Reserva 1 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Reserva_1,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Reserva 2 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Reserva_2,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Reserva 3 =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Reserva_3,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Freno =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_Freno,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt Fallos Encoder =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Fallos_C_Encoder,8,0) & ";" & Chr(10) & Chr(13)
    Ayuda = Ayuda & "Cnt PE Encoder =;;;;;;"
    Ayuda = Ayuda & NumberFormat(B4XPages.MainPage.ES_CNT_Fallos_seg_Encoder,8,0) & Chr(10) & Chr(13)
    
    ' Guarda en Fichero Android\data\
    File.WriteString(Ruta, Fichero, Ayuda)
        
    ' Presenta Informacion
    Log("Final Exportacion")
    Log("Ruta =" & Ruta )
    Log("Fichero =" & Fichero)
    Log("Largo Fichero  =" & File.Size(Ruta,Fichero))
    
    ' Activa Boton Exportar
    
    LB_Exportando.Visible     = False
    B_Exporta.Visible         = False
    B_Exporta.Enabled        = True
    
    Log("Abre Ventana para Guardar como el Fichero")
    
    Wait For (B4XPages.MainPage.SaveAs(File.OpenInput(Ruta,Fichero), "application/octet-stream", Fichero)) Complete (Success As Boolean)
    If Success = True Then
        Control.LogMessage("* ","Fichero Guardado Correctamente")
        ToastMessageShow("Fichero Creado correctamente",True)
    Else
        Control.LogMessage("* ","Error al Guardar el Fichero")
        ToastMessageShow("Error al crear el fichero de parametros",True)
    End If

End Sub


The file in the original path is always correct, the problem is in SaveAs that generates a file but empty, sometimes it does it well, but approximately one in 10.



Any help?
 
Upvote 0

asales

Expert
Licensed User
Longtime User
The file in the original path is always correct, the problem is in SaveAs that generates a file but empty, sometimes it does it well, but approximately one in 10.

Any help?
Try to change:
B4X:
Ruta = File.DirDefaultExternal
to
B4X:
Ruta = File.DirInternal
 
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
I have already changed the path to all possible ones and the result is the same

Before SaveAs the file is always saved correctly with all the data. Sometimes SaveAs saves the file with the data but normally the empty file is created, it is the chosen location and with the assigned name, but the file is completely empty.

This is driving me crazy and I can't find any explanation. In the same program I have two other calls to SavaAS and they work correctly. I make these calls from a Class module while this call is made from a code module. That's the only difference.

I'm out of ideas as to where the problem might be or what other tests to perform. Any more help?

Thank you very much
 
Upvote 0
Top