Where is the right place to store your app's settings?

priitp

Member
Licensed User
Longtime User
Currently I do save some settings and temp files at "File.DirDefaultExternal".
But for phones without SD card the solution doesn't work.
Also checked "Can isntall to external storage".

I wonder if there is a universal path available what phone OS itself will direct to internal memory or SD depending on available recources.

I know the trick to greate virtual SD card but looks weird to mess around so much with so trivial problem.

Priit
 

rfresh

Well-Known Member
Licensed User
Longtime User
TextWriter works well. You can check for an SD Card and then write to DirInternal or DirExternal.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Is there a way to map "DirInternal" to "DirRootExternal" or to "DirDefaultExternal" when the device has no SD card, so it can be recognized as an external folder. This way, you can accommodate devices that do not have a slot for an SD. Something like:
B4X:
If no external card then
  'map internal to an external DirRootExternal or DirDefaultExternal
else
  'No mapping is required and SD card will hold files
end if
DBFilePath = File.DirRootExternal & "/" & MyFolder
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I use this:

B4X:
If File.ExternalWritable = True Then
      'SD Card Installed
      pFilePath = File.DirRootExternal
   Else
      'No SD Card
      pFilePath = File.DirInternal
   End If
   If File.Exists(pFilePath, pDisplay_dat_filename) = True Then
 
Upvote 0

Andras

Active Member
Licensed User
Longtime User
Useful, but in this example what Type is pFilePath? I'd assumed it's a string variable, but using it as such gives an error message, and it won't compile as Type Path either.

Thanks!

John
 
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
This raises the question about directories in internal memory. We have:

- File.DirDefaultExternal
- File.DirRootExternal
but only
- File.DirInternal

Does this mean that apps using internal storage don't follow the /Android/data/Appname format and store everything in one big directory?

Douglas
 
Upvote 0

priitp

Member
Licensed User
Longtime User
Thanks for help.

Made something like this:

B4X:
Sub Process_Globals
   Dim SettingsFolder As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      If File.ExternalWritable = True Then
         SettingsFolder = File.DirDefaultExternal
      Else
         SettingsFolder = File.DirInternal
      End If
   End If
End Sub

Looks working so far.

I wonder a bit as this is so common problem that may be it is wize to have it built in to Basic4android as a readonly string variable.

Thanks.
Priit
 
Upvote 0

Osi

Member
Licensed User
Longtime User
Depends on the device unfortunately. For instance, on the Kindle Fire, there is no DirInternal, DirExternal directories. Everything must go under DirAssets.

I may have that switched around, but 2 of those three do not exist.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
>I wonder a bit as this is so common problem that may be it is wize to have
>it built in to Basic4android as a readonly string variable.

No because some developers will use only one specific folder and two, not predefining it lets you choose your own, meaningful name.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
The kindle fire has all of the File.Dir attributes. Also, you can not use File.DirAssets as this folder is Read Only. File.DirInternal works fine on the kindle fire and this is the default data directory structure and is what should be used to protect the data as other apps can not access this information. The attached image is an example of the folder information returned under each File.Dir attribute on the kindle fire:
 

Attachments

  • kf.jpg
    kf.jpg
    44.8 KB · Views: 256
Last edited:
Upvote 0

kanaida

Active Member
Licensed User
Longtime User
That's what I used to do before I found out about the Settings Manager. It's the b4a equivalent to vb.net's My.Settings pretty much. It just knows where to save :) If I remember right it's in the PreferenceActivity library.

B4X:
Dim m as Manager  (can't remember the exact class name)

m.setstring("setting1","Somevalue")
m.setBoolean("setting1","Somevalue")

m.getstring("setting1")
m.getBoolean("setting1")
 
Upvote 0

Andras

Active Member
Licensed User
Longtime User
Yes, it's a global string:

B4X:
Dim pFilePath As String

Right, that's what I'd initially assumed, but I still have a problem here.

With pFilePath duly DIMmed as a global string, the following code derived directly from your example still throws up an error:

B4X:
If File.ExternalWritable = True Then
     '   SD Card Installed
        pfilepath = File.DirDefaultExternal
    Else
     '   No SD Card
        pfilepath = File.DirInternal
    End If
Info = (DateTime.Date(File.LastModified(File.pfilepath, "places.db")))

The error is:

B4X:
Error compiling program.
Error description: Unknown member: pfilepath
Occurred on line: 61
Info = (DateTime.Date(File.LastModified(File.pfilepath, "places.db")))
Word: pfilepath

Any ideas?

Thanks!

John
 
Upvote 0

Jost aus Soest

Active Member
Licensed User
Longtime User
File.pfilepath does not exists! ;)

So I think you wanted this:
B4X:
Info = DateTime.Date(File.LastModified(pFilePath, "places.db"))
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I am using a device with no SD card. I saved a small text file to File.DirInternal. When I display the path on the device with a message box, it shows me:
/data/data/mypackagename/files. I looked for the file on the device, but could not find it. I searched everywhere using a file manager. The device is a HTC Droid Incredible with no SD card.
Can anybody help me unvail this mystery?
Thanks
 
Upvote 0
Top