Android Question How do I resolve? Problems with MAP files..

Levisvv

Member
Licensed User
Longtime User
So I discovered that the best way to load and save settings is to use MAP files, fine.
In my load I initialize Map1 as a Map and then call my subs
So I created 2 subs, SaveIni() and this one:

Sub LoadIni(fPath As String,filename As String)
Dim i As Int
'Usage: 'myhost = LoadIni(File.DirDefaultExternal,"MYsettings.ini")
Map1 = File.ReadMap(fPath,filename)
For i = 0 To Map1.Size - 1
If Map1.GetKeyAt(i) = "ServerIP" Then
ServicesvrIP = Map1.GetValueAt(i)
Else If Map1.GetKeyAt(i) = "ServerPort" Then
ServicesvrPORT = Map1.GetValueAt(i)
Else If Map1.GetKeyAt(i) = "LocalID" Then
ServiceARCid = Map1.GetValueAt(i)
Else If Map1.GetKeyAt(i) = "RingToneToUse" Then
SelectedTone = Map1.GetValueAt(i)
Else If Map1.GetKeyAt(i) = "PasswordLockToUse" Then
LockoutPassword = Map1.GetValueAt(i)
End If
Next
End Sub

My app is crashing and in the logs I get:
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)

What am I doing wrong?
How can I check for the file and create it if it does not yet exist ?
 

DouglasNYoung

Active Member
Licensed User
Longtime User
Levisvv,
The error is clear that the file does not exist - Posting the SaveIni sub might just help as the problem is most certainly there! Also it makes code clearer if you use the CODE flags ->
B4X:
Code looks like this

Douglas
 
Upvote 0

Levisvv

Member
Licensed User
Longtime User
B4X:
Sub LoadIni(fPath As String,filename As String)
    Return
    'ReadIni(key As String,fPath As String,filename As String)
    Dim i As Int
    'To read above Record use this:
    'myhost = ReadIni("HOST",File.DirDefaultExternal,"ARC.ini")
    Map1 = File.ReadMap(fPath,filename)
    For i = 0 To Map1.Size - 1
      If Map1.GetKeyAt(i) = "ServerIP" Then
        ServicesvrIP = Map1.GetValueAt(i)
      Else If Map1.GetKeyAt(i) = "ServerPort" Then
        ServicesvrPORT = Map1.GetValueAt(i)
      Else If Map1.GetKeyAt(i) = "LocalID" Then
        ServiceARCid = Map1.GetValueAt(i)
      Else If Map1.GetKeyAt(i) = "RingToneToUse" Then
        SelectedTone = Map1.GetValueAt(i)
      Else If Map1.GetKeyAt(i) = "PasswordLockToUse" Then
        LockoutPassword = Map1.GetValueAt(i)
      End If
    Next
End Sub
 
Upvote 0

Levisvv

Member
Licensed User
Longtime User
B4X:
Sub SaveIni(fPath As String,filename As String)
    Return
    'WriteIni(mykey As String,myvalue As String,fPath As String,filename As String)
    'To write record HOST=127.0.0.1 to ARC.ini, make this Call:
    'WriteIni("HOST","127.0.0.1",File.DirDefaultExternal,"ARC.ini")
    If Not(File.Exists(fPath,filename)) Then File.WriteMap(fPath, filename, Map1)  'to create it if not exist
    Map1 = File.ReadMap(fPath,filename)
            Map1.Put("ServerIP", ServicesvrIP)
            Map1.Put("ServerPort", ServicesvrPORT)
            Map1.Put("LocalID", ServiceARCid)
            Map1.Put("RingToneToUse", SelectedTone)
            Map1.Put("PasswordLockToUse", LockoutPassword)
    File.WriteMap(fPath, filename, Map1)
End Sub
 
Upvote 0

Levisvv

Member
Licensed User
Longtime User
OK so I added some code to check for and create the file if it does not exist, but I still get the same error.
WTF??


B4X:
Sub LoadIni(fPath As String,filename As String)
    'Return
    'first check if the file exists, if not save the defaults
    If File.Exists(fPath,filename) = False Then
        'default parameters
        ServicesvrIP    = "192.168.1.121"
        ServicesvrPORT  = 7234
        ServiceARCid    = "Android ARC"
        SelectedTone    = "Tone 1"
        LockoutPassword = "1234"
        SaveIni(fPath,filename)
    End If
    'ReadIni(key As String,fPath As String,filename As String)
    Dim i As Int
    'To read above Record use this:
    'myhost = ReadIni("HOST",File.DirDefaultExternal,"ARC.ini")
    Map1 = File.ReadMap(fPath,filename)
    For i = 0 To Map1.Size - 1
      If Map1.GetKeyAt(i) = "ServerIP" Then
        ServicesvrIP = Map1.GetValueAt(i)
      Else If Map1.GetKeyAt(i) = "ServerPort" Then
        ServicesvrPORT = Map1.GetValueAt(i)
      Else If Map1.GetKeyAt(i) = "LocalID" Then
        ServiceARCid = Map1.GetValueAt(i)
      Else If Map1.GetKeyAt(i) = "RingToneToUse" Then
        SelectedTone = Map1.GetValueAt(i)
      Else If Map1.GetKeyAt(i) = "PasswordLockToUse" Then
        LockoutPassword = Map1.GetValueAt(i)
      End If
    Next
End Sub
 
Upvote 0

Levisvv

Member
Licensed User
Longtime User
I am calling it with this in my service create routine:

B4X:
'load the ARC settings..
    LoadIni (File.DirDefaultExternal,"ARC.ini")
 
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
Levisvv,
May not be the only problem but. . . The code in post #6 : SaveIni immediately performs a Return, thus the SaveIni sub does nothing and never saves the map, hence File not found?

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