Android Question DirDefaultExternal Issue

Chris Lee

Member
Licensed User
Hi,

Here is a weird one.
I have a Galaxy S7 running Android 7.0 Nougat.
B4A 7.01 SDK platform 25

In all cases in this post, I'm running from the B4A development environment in 'Debug' compilation mode.

Everything was working well creating my SQL database using:

SQL.Initialize(File.DirDefaultExternal,"SQLFile.db",True)

File.DirDefaultExternal is returning:

/storage/emulated/0/Android/data/myapp/files

The SQLFile.db was written correctly to this folder.

As part of my testing, I wondered what would happen if I cleared the data and cache via the settings > Apps > MyApp > Storage (clear data) and (clear cache) options.

Obviously I was expecting the database to be wiped out, which it was.
However I was expecting when running the application again it would likely re-create the external folder.
It says on the method documentation that calling File.DirDefaultExternal will create the folder if required: 'The folder is created if needed'

However it didn't.

What I actually got when calling the SQL.Initalize as above was:

Error Code : 1294 (SQLITE_CANTOPEN_ENOENT)
Caused By : Specified directory or database file does not exist.
(unknown error (code 1294): Could not open database)

File.DirDefaultExternal is still returning:

/storage/emulated/0/Android/data/myapp/files

File.ExternalReadable is returning 'True'

When I check for this folder, that I could previously see in Solid Explorer at:

Internal Memory/Android/data that the folder is missing.

Uninstall?

OK, so I guess what I did screwed things up a little and because the application is already installed, it thinks the folder already exists. So I uninstalled my application.

After uninstall I ran the project again, with the assumption that this would likely fix the issue.

However at this point I'm still getting the same error on SQL.Initalize and the folder doesn't exist.

I wondered what would happen if I tried something like:

File.MakeDir("/storage/emulated/0/Android/data","myapp/files")

Which didn't error but didn't create the folder either.

Manual folder creation


Hmm?

So I decided to manually recreate the folder from within solid explorer at:

Internal Memory/Android/data

Adding folder myapp and creating a files folder within that.

Internal Memory/Android/data/myapp/files

Now when I run everything startup up fine and database is created.

Uninstall again.

So now I unstall the application again (the folder disappears as you would expect)
This time I didn't clear the cache, and run again.
I get the SQL error again.
Uninstalled again and restart the phone for good measure.
Ran again and folder doesn't get created.

So it seems that android or B4A is hanging onto the idea that the folder already exists, but obviously I don't know the exact interactions going on here.

Trying Internal

File.DirInternal didn't have this issue, I could clear the cache and data, uninstall etc and it would just go ahead and re-create it.

Conclusions

OK, so I don't know how often the average user clears the cache of an application, and I don't know if this would still occur if I had simply had uninstalled it.

At the moment it seems like a safe bet to use the internal storage option instead. Although I may be just seeing an artifact of being in the development environment, since I've not looked to see what happens if I use an APK.

Seems like somehow it's holding on the assumption that the folder exists.

Any ideas what might be going on here?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
At the moment it seems like a safe bet to use the internal storage option instead. Although I may be just seeing an artifact of being in the development environment, since I've not looked to see what happens if I use an APK.
There is no difference.

What is the targetSdkVersion set to?
 
Upvote 0

metzeller_2013

Member
Licensed User
Longtime User
Seems I have the same Problem creating directories...

B4X:
Dim dir As String

Msgbox( rp.GetSafeDirDefaultExternal("DATA_IN"),"In")   'this one created the folder in defaultexternal in the internal memory and in the SD card 2 folder where created.
Msgbox( rp.GetSafeDirDefaultExternal("DATA_OUT"),"Out") 'this one too

dir = rp.GetSafeDirDefaultExternal("")

File.MakeDir(dir,"BACKUP") ' this one I tried only created a folder in the internal memory not in the SD card
 
Upvote 0
Top