I'm trying to store 5 numeric variables in persistent storage on my external sd card- and failing miserably.
I use the line
FuelDataStore.Initialize(File.DirDefaultExternal, "FuelDataStore")
but it fails to create the file, so I have done something wrong, I just can't figure out what. I have given explicit read and write permission in the manifest, and it asks for the permissions on installation, so I don't think it is a permissions problem.
I have searched the forums and all the manuals and code snippets I can find to no avail- somebody please put this beginner out of his misery!
The whole code for my app is here-
I would really appreciate some help on this.
Thanks, Ken.
I use the line
FuelDataStore.Initialize(File.DirDefaultExternal, "FuelDataStore")
but it fails to create the file, so I have done something wrong, I just can't figure out what. I have given explicit read and write permission in the manifest, and it asks for the permissions on installation, so I don't think it is a permissions problem.
I have searched the forums and all the manuals and code snippets I can find to no avail- somebody please put this beginner out of his misery!
The whole code for my app is here-
B4X:
#Region Project Attributes
#ApplicationLabel: Fuel Consumption
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: 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.
Public FuelDataStore As KeyValueStore
Dim current_fuel As Float
Dim odo As Float
Dim old_odo As Float
Dim total_fuel As Float
Dim oa_consumption As Float
Dim current_consumption As Float
Dim total_odo As Float
Dim start_odo As Float
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.
Public Calc_click As Button 'crunch the numbers
Public Calculate As Button
Public CurrentConsumption As Label
Public Fuel As Label
Public FuelConsumptionAverage As Label
Public FuelConsumptionThisTank As Label
Public FuelEntry As EditText
Public Odometer As Label
Public OdometerEntry As EditText
Public OverallConsumption As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Select File.Exists(File.DirDefaultExternal, "FuelDataStore")
Case True
' If the file exists, do nothing
Case False
FuelDataStore.Initialize(File.DirDefaultExternal, "FuelDataStore")
current_consumption = 0
oa_consumption = 0
total_fuel = 0
start_odo = 0
old_odo = 0
FuelDataStore.Put("oa_consumption", "oa_consumption")
FuelDataStore.Put("current_consumption", "current_consumption")
FuelDataStore.Put("total_fuel", "total_fuel")
FuelDataStore.Put("start_odo", "start_odo")
FuelDataStore.Put("old_odo", "old_odo")
End Select
Activity.LoadLayout("fuellayout")
OdometerEntry.Text = "0"
FuelEntry.Text = "0"
End Sub
Sub Activity_Resume
' load all variables in this sub
StateManager.RestoreState(Activity, "Main", 20)
oa_consumption = FuelDataStore.Get(oa_consumption)
current_consumption = FuelDataStore.Get(current_consumption)
total_fuel = FuelDataStore.Get(total_fuel)
start_odo = FuelDataStore.Get(start_odo)
old_odo = FuelDataStore.Get(old_odo)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
StateManager.ResetState("Main")
Else
StateManager.SaveState(Activity, "Main")
End If
' save all variables in this sub
FuelDataStore.Put(oa_consumption, oa_consumption)
FuelDataStore.Put(current_consumption, current_consumption)
FuelDataStore.Put(total_fuel, total_fuel)
FuelDataStore.Put(start_odo, start_odo)
FuelDataStore.Put(old_odo, old_odo)
End Sub
Sub Calculate_click
current_fuel = FuelEntry.Text
odo = OdometerEntry.Text
If odo = old_odo Then Return
Select current_fuel
Case 0
current_consumption = 0
oa_consumption = 0
total_fuel = 0
old_odo = OdometerEntry.Text
start_odo = OdometerEntry.Text
FuelDataStore.Put(start_odo, start_odo)
FuelDataStore.Put(old_odo, old_odo)
Case Else
total_fuel = total_fuel + current_fuel
odo = OdometerEntry.Text
total_odo = (odo - start_odo)
current_consumption = current_fuel/((odo - old_odo)/100)
oa_consumption = total_fuel/(total_odo/100)
old_odo = odo
CurrentConsumption.Text = NumberFormat(current_consumption, 0,1)
OverallConsumption.Text = NumberFormat(oa_consumption, 0,1)
FuelDataStore.Put(oa_consumption, oa_consumption)
FuelDataStore.Put(current_consumption, current_consumption)
FuelDataStore.Put(total_fuel, total_fuel)
FuelDataStore.Put(start_odo, start_odo)
FuelDataStore.Put(old_odo, old_odo)
End Select
End Sub
I would really appreciate some help on this.
Thanks, Ken.