Android Question not save txt, whi?

fifiddu70

Well-Known Member
Licensed User
Longtime User
sorry, i have this problem when save this file, see picture
 

Attachments

  • 1.jpg
    25.9 KB · Views: 217
Upvote 0

sorex

Expert
Licensed User
Longtime User
seems more like you're reading a file than writing one.

how can we help without seeing your code?
 
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
B4X:
#Region  Project Attributes
    #ApplicationLabel: Cassa
    #VersionCode: 1
    #VersionName: 1.0
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: True   
    #BridgeLogger: True
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Serial1 As Serial
    Dim TextReader1 As TextReader
    Dim Timer1 As Timer
    Dim timer2 As Timer
    Dim timer3 As Timer
    Dim connected As Boolean
    Dim mp1 As MediaPlayer
    Dim BtAdmin As BluetoothAdmin
    Dim BTConnection As Serial
    Dim Printer As TextWriter
    Dim p As Phone
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim txtLog As EditText
    Dim txtSend As EditText
    Private btnSend As Button
    Private pnlbase As Panel
    Private txttotaleeuro As EditText
    Dim k As Int

    Private txttavolo As EditText
    Private txttavolozza As EditText
    Private txtdisplay As EditText
End Sub

Sub Activity_KeyPress (KeyCode As Int)  As Boolean
    Dim answ As Int
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        answ = Msgbox2("Sei sicuro di voler uscire dal programma?","AVVISO!!","SI","","NO",Null)
        If answ = DialogResponse.POSITIVE Then
            ExitApplication
            Return False
        Else
            Return True
   
        End If
    End If
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    If FirstTime Then
        Serial1.Initialize("Serial1")
        BtAdmin.Initialize("BlueTooth")
        BTConnection.Initialize("Printer")
    End If
    Activity.LoadLayout("cassa")
    Timer1.Initialize("Timer1", 200)
    timer2.Initialize("timer2",8000)
    timer3.Initialize("timer3",2000)
    mp1.Initialize
    p.SetScreenOrientation(1)
    pnlbase.SetLayout(0%x,0%y,100%x,100%y)
       
    txtLog.SetLayout(1%x,1%y,98%x,50%y)
    txttavolozza.SetLayout(1%x,72%y,15%x,8%y)
    txttavolo.SetLayout(55%x,72%y,15%x,8%y)
    txttotaleeuro.SetLayout(73%x,72%y,25%x,8%y)
    txtSend.SetLayout(1%x,90%y,70%x,8%y)
    btnSend.SetLayout(73%x,90%y,25%x,8%y)
   
   
    txtLog.Color=Colors.White
    txtSend.Color=Colors.White
    txttotaleeuro.Color=Colors.White
    txttavolo.Color=Colors.White
    txttavolozza.Color=Colors.White
End Sub

Sub Activity_Resume
    If Serial1.IsEnabled = False Then
        Dim i As Intent
        i.Initialize("android.settings.BLUETOOTH_SETTINGS", "")
        StartActivity(i)
    Else
        Serial1.Listen 'listen for incoming connections
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Serial1_Connected (Success As Boolean)
   
    Dim TextWriter1 As TextWriter
    If Success Then
        ToastMessageShow("Connessione riuscita", False)
        TextReader1.Initialize(Serial1.InputStream)
        TextWriter1.Initialize(Serial1.OutputStream)
       
        Timer1.Enabled = True
        connected = True
       
       
           
       
    Else
        connected = False
        Timer1.Enabled = False
        Msgbox(LastException.Message, "Errore di connessione.")
       
    End If
       
   
End Sub
Sub Timer1_Tick
    If connected Then
       
        If TextReader1.Ready Then 'check if there is any data waiting to be read
            txtLog.Text = txtLog.Text & TextReader1.ReadLine & CRLF
            txtLog.SelectionStart = txtLog.Text.Length
            txtdisplay.Text=txtLog.Text
            k = txtLog.Text.IndexOf("Tavolo:")' eseguo la ricerca di Tavolo: per poter estrapolare solo la cifra da inserire nella txteuro.text
            If k >= 0 Then
                txttavolo.Text = txtLog.Text.SubString2(k + 7, txtLog.Text.Length)' da qui estrapolo il tavolo
                txttavolozza.Text=txttavolo.Text
               
            End If
        End If
    End If
   
   
End Sub
Sub txtLog_TextChanged (Old As String, New As String)
    If txtdisplay.Text = txtLog.Text Then
       
    Else
        timer2.Enabled=True
    End If
End Sub
Sub timer2_Tick
   
   
    timer2.Enabled=False
    timer3.Enabled=True
End Sub

Sub timer3_Tick
    Timer1.Enabled=False
    btnSend_Click
   
    timer3.Enabled=False
End Sub

Sub btnSend_Click
   
        Dim PairedDevices As Map
        PairedDevices = BTConnection.GetPairedDevices
        Dim l As List
        Dim DeviceName, MacAddress As String
        l.Initialize
        For i = 0 To PairedDevices.Size - 1 'Check all devices
            l.Add(PairedDevices.GetKeyAt(i))
            DeviceName=PairedDevices.Getkeyat(i)
            MacAddress=PairedDevices.GetValueAt(i)
            Log(DeviceName & " -> " & MacAddress)
            If DeviceName.Contains("Thermal") Then 'Insert the BT-Name of the printer or use the MAC address
                Exit
            End If
        Next
        BTConnection.Connect(MacAddress)
   
    conteggia
'    If connected Then
        'TextWriter1.WriteLine(txtSend.Text)
        'TextWriter1.Flush
        'txtSend.Text = ""
        'Else
        'Msgbox ("CONTROLLA SE: SEI CONNESSO, DEVI INSERIRE UN TESTO O REINSERIRE UN'IMMAGINE PRIMA DI INVIARE","CAMPO VUOTO")
    'End If
End Sub
Sub conteggia
    File.WriteString(File.DirRootExternal,txttavolo.Text & ".txt", txtLog.Text)
   
    txtLog.Text=""
       
    Timer1.Enabled=True
   
End Sub
Sub Printer_Connected (Success As Boolean)
    If Success Then
        Printer.Initialize2(BTConnection.OutputStream,"windows-1252") 'important to print f.e. German/French chars
    '    PrintBuffer=Chr(27)&"t"&Chr(16)&" !!!! " 'Set codepage 1252
        Printer.WriteLine(txtdisplay.Text)
        Printer.Flush

        Msgbox("Printed!","")
        Printer.Close
        BTConnection.Disconnect 'disable this if you like
        'btnDisconnetti_Click
        timer3.Enabled=True
       
       
    Else
        Msgbox("No printer found...","Print error")
    End If
End Sub


Sub txttavolo_TextChanged (Old As String, New As String)
   
End Sub
 
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
i have write first this code for save txt file:
B4X:
Sub conteggia
    Dim scrivi As TextWriter
    scrivi.Initialize( File.OpenOutput(File.DirRootExternal, "tavolo" & txttavolo.Text & ".txt",True))
    scrivi.WriteLine(txtLog.Text & CRLF)
    scrivi.Close
   
    txtLog.Text=""
       
    Timer1.Enabled=True
   
End Sub
but not save, i have indentical error.
 
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
yes, if make: scrivi.Initialize( File.OpenOutput(File.DirRootExternal, "tavolo.txt",True)) ok
this is not ok:
scrivi.Initialize( File.OpenOutput(File.DirRootExternal, "tavolo" & txttavolo.Text & ".txt",True))
B4X:
Sub conteggia
    Dim scrivi As TextWriter
    scrivi.Initialize( File.OpenOutput(File.DirRootExternal, "tavolo.txt",True))
    scrivi.WriteLine(txtLog.Text & CRLF)
    scrivi.Close
  
    txtLog.Text=""
      
    Timer1.Enabled=True
  
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
but is txttavolo.txt containing any text? maybe it's null since K in that check has a wrong value?
 
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
because if save with name: tavolo.txt this file is created with text in to the file but if save with name: "tavolo" & txttavolo.text & ".txt",true not savethis file and show this error in picture?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I'm talking about this part here

B4X:
k = txtLog.Text.IndexOf("Tavolo:")' eseguo la ricerca di Tavolo: per poter estrapolare solo la cifra da inserire nella txteuro.text
            If k >= 0 Then
                txttavolo.Text = txtLog.Text.SubString2(k + 7, txtLog.Text.Length)' da qui estrapolo il tavolo
                txttavolozza.Text=txttavolo.Text
              
            End If

if there is no Tavolo: in the log that label text won't be updated and your file becomes .txt not sure if andoird allows filenames to starts with a dot.
 
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
i see in txttavolo.text and txttavolozza.text the number of tavolo, but non save with Tavolo & txttavolo.text & ".txt"
 
Upvote 0

alan1968

Active Member
Licensed User
Longtime User
hi check this code

B4X:
Sub conteggia
    Dim scrivi As TextWriter
    Dim filetavolo As String
    filetavolo="tavolo" & txttavolo.Text.Trim & ".txt"
    scrivi.Initialize( File.OpenOutput(File.DirRootExternal, filetavolo,True))
    scrivi.WriteLine(txtLog.Text & CRLF)
    scrivi.Close
  
    txtLog.Text=""
      
    Timer1.Enabled=True
  
End Sub
 
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
Alan1968 i try your code but receive this error:

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Thermal Printer -> DC:0D:30:02:6F:C9
main_vvv3 (java line: 507)
java.io.FileNotFoundException: /storage/emulated/0/tavolo68
Pizzeria
1 : Pizza Belvedere - Euro 5.00

===========================

*COMANDA Numero: 33 - 25/03/2017 - ORE:19.44
Operatore: Anonimo - Tavolo: 68
Primi
Note:
TOTALE EURO: 5.00.txt: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:370)
at com.partannasoftware.cassa.main._vvv3(main.java:507)
at com.partannasoftware.cassa.main._printer_connected(main.java:555)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
at anywheresoftware.b4a.BA$2.run(BA.java:338)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:920)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:715)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:442)
... 16 more
 
Upvote 0

alan1968

Active Member
Licensed User
Longtime User
B4X:
Sub conteggia
    Dim scrivi As TextWriter
    Dim filetavolo As String
    Dim foldertavolo As String
    foldertavolo=File.DirRootExternal & "/Tavolo"
    filetavolo="tavolo" & txttavolo.Text.Trim & ".txt"
    scrivi.Initialize( File.OpenOutput(foldertavolo, filetavolo,True))
    scrivi.WriteLine(txtLog.Text & CRLF)
    scrivi.Close

    txtLog.Text=""
    
    Timer1.Enabled=True

End Sub
 
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
no, i use android 5.1.1
its a very hardest to resolve this problem.
receive this error with to your new code:

** Activity (main) Resume **
Thermal Printer -> DC:0D:30:02:6F:C9
main_vvv3 (java line: 512)
java.io.FileNotFoundException: /storage/emulated/0/Tavolo/tavolo66
Pizzeria
1 : Pizza Belvedere - Euro 5.00

===========================

*COMANDA Numero: 37 - 25/03/2017 - ORE:19.44
Operatore: Anonimo - Tavolo: 66
Primi
Note:
TOTALE EURO: 5.00.txt: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:370)
at com.partannasoftware.cassa.main._vvv3(main.java:512)
at com.partannasoftware.cassa.main._printer_connected(main.java:560)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
at anywheresoftware.b4a.BA$2.run(BA.java:338)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:920)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:715)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:442)
... 16 more
 
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
because this error says: open failed: ENOENT (No such file or directory)
open failed is when reader file ? i write file not read
 
Upvote 0

alan1968

Active Member
Licensed User
Longtime User
B4X:
Sub conteggia
    Dim scrivi As TextWriter
    Dim filetavolo As String
    Dim foldertavolo As String
    Try
    foldertavolo=File.DirRootExternal & "/Tavolo"
    filetavolo="tavolo" & txttavolo.Text.Trim & ".txt"
    scrivi.Initialize( File.OpenOutput(foldertavolo, filetavolo,True))
    scrivi.WriteLine(txtLog.Text & CRLF)
    scrivi.Close
    txtLog.Text=""     
    Timer1.Enabled=True
    Catch
    Log(filetavolo)
    Log(foldertavolo)
    End Try   
 
End Sub

java.io.FileNotFoundException: /storage/emulated/0/Tavolo/tavolo66

TOTALE EURO: 5.00.txt: open failed: ENOENT (No such file or directory)

LOG ?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…