DirRootExternal for all file actions

jschuchert

Active Member
Licensed User
Longtime User
For a while now I have been agonizing over the internal/external memory/sdcard issues for which many opinions have been expressed. My application generates text databases of numbers and was wishfully designed to copy those databases back and forth between device and computer which was a critical part of the app. However, it didn’t work the way I had hoped. Occasionally I could copy an external db to the device and find it again but generally not and from the device to the computer even less frequently. After reading some posts that explained ‘external’ really didn’t mean an sdcard to which I thought I had been writing data, in desperation I replaced all references of DirInternal in my code to DirRootExternal so there would be only 1 repository for data. Something worked, for now I can copy the databases back and forth as planned. I tested it on devices with and without sdcards and it seemed to be ok. In essence, all files are opened in and written to DirRootExternal. I will do more testing but does anyone see any problems down the road with this approach or have other suggestions?

Incidentally, I downloaded “Wifi File Transfer” from google play and it makes exploring and copying files between device and computer a snap over a wifi connection. No more usb cable.

Jim S.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
In most cases it is better to use File.DirDefaultExternal. This is the place for your app.

You should check File.ExternalWritable.

Check this sub from DBUtils for example:
B4X:
Sub CopyDBFromAssets (FileName As String) As String
   Dim TargetDir As String
   If File.ExternalWritable Then TargetDir = File.DirDefaultExternal Else TargetDir = File.DirInternal
   If File.Exists(TargetDir, FileName) = False Then
      File.Copy(File.DirAssets, FileName, TargetDir, FileName)
   End If
   Return TargetDir
End Sub
In all other code TargetDir is used.
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
I tested it on devices with and without sdcards and it seemed to be ok.

Just because it lacks an sdcard doesn't mean it lacks external storage; many devices, particularly HTC ones (pun intended), lack an actual sdcard or slot for one, but they have an internal ROM which Android sees as the sdcard. I don't believe your app can tell the difference.

Have you really tried it on a device without external storage? To do so you would need to get a device without an internal storage ROM, one that has an sdcard, and then remove the sdcard and test your app.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Thanks, Erel and yttrium,

That is very useful information that I can use. I have no device without external storage. The devices I tested were Toshiba Thrive (has sdcard), Archos 7 G2 (has sdcard), Nexus 7 (no sdcard or slot). All 3 show the databases I created (with or without card) when the device is connected via wifi to either "wifi file explorer", "wifi file transfer" or "wellftp server". I certainly appreciate your advice and expertise.

Jim S.
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
All 3 show the databases I created (with or without card) when the device is connected via wifi to either "wifi file explorer", "wifi file transfer" or "wellftp server".

You mean that the two devices with sdcards work fine without said cards?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
One of my tablets had an SDCard slot AND additional inbuilt ram that was labelled NAND installed, which was used in preference to the SD card. Apps were installed to that additional memory by default, so unfortunately the situation can be complex.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Yes. On the Thrive, I created a file with and one without the sdcard insserted. They both were residing in
"/mnt/sdcard/Android/data/android.b4a.cogo4droideval7/files/"

On the archos 7, the file created without the sdcard was in
"mnt/storage/sdcard/Android/data/android.b4a.cogo4droideval7/files"
When I inserted the card, I had to create a new file.

Jim S.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Thank you, Steve for your input,
I agree about the complexity due to the plethora (I love that word) of devices out there. When I update the application, I will explain to my users in the manual about the variables which could cause problems. I am optimistic that eventually a standard will be employed.

Jim S.
 
Upvote 0
Top