Android Question KeyValueStore.Initialize won't create file

vk7krj

Member
Licensed User
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-

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.
 

vk7krj

Member
Licensed User
Erel-

From the manifest- android:targetSdkVersion="14"/>

and I tried changing File.DirDefaultExternal to RuntimePermissions.GetSafeDirDefaultExternal("") and got a syntax error. "Undeclared variable 'runtimepermissions' is used before it was assigned any value". I had a look at the link and it says (in part)

If the targetSdkVersion is lower than 23 then the standard permissions system will be used on all devices including Android 6+.

so I think that means I don't actually need to make the change?

LucaMs, I read your link, I have this line in the manifest

AddPermission(android.permission.write_external_storage)

and when the app installs it asks for read & write permission for the external card so I assume it is not a permissions problem. But then again- it doesn't work....

Ken.
 
Upvote 0

vk7krj

Member
Licensed User
I used Airdroid to look at the external sd card, under Android/data/ and could find nothing that looked related to my app. I also did a file search of the whole card for "FuelDataStore" and found nothing.

I forgot to add to the original post, when I start the app I get the error-

An error has occurred in sub:KeyValueStore_get
(java line 76)
java_lang.nullpointerexception
attempt to invoke virtual methed
android.database.cursor
anywheresoftware.B4A.sql.SQL.execquery2(java lang.string, java.lang.string[])" on a null object reference

The above is not verbatim, I can't find a way to copy & paste from the phone.

I am assuming that it means it is failing to find the data I am trying to load on startup?
 
Upvote 0
Top