Android Question Writing Files to internal memory

brianh

Member
Licensed User
Another "dummies" question from a B4A newbie which probably has a simple answer.

In a B4A app I am trying to write a file to internal memory but I keep getting errors. I want to use internal memory as I might be switching SD cards so want the data there all the time. 2 Samples of my code are:-

Code example 1:-
Dim list1 As List
Dim directory1, testfile As String
directory1 = File.DirRootExternal
testfile1 = "testfile1.txt"
File.WriteList(directory1, testfile1, list1)

Code example 2:-
Dim list1 As List
Dim directory1, testfile As String
directory1 = "/"
testfile1 = "testfile1.txt"
File.WriteList(directory1, testfile1, list1)


The first example writes perfectly OK to external memory (as I can detect it using File.Exists) but the 2nd one for the internal memory location generates errors. I have tried different paths but they all cause errors unless I use File.DirInternal then it's OK (but I understand that I cannot then access the saved file outside the app).


The code above is based on the "Android Tutorial - Text Files" dated Nov 28 2010 so I'm not sure if those code examples are still valid.

Many thanks

Brian
 

Mahares

Expert
Licensed User
Longtime User
I have tried different paths but they all cause errors unless I use File.DirInternal then it's OK (but I understand that I cannot then access the saved file outside the app).
Your statement is correct. Use File.DirInternal. , But if you want to access the file outside its app, you need to use File.DirRootExternal
 
Upvote 0

brianh

Member
Licensed User
Mahares

By playing around (and looking at many other answers), I've found I can get it working by using "/storage/emulated/0/Data/{..etc.....}/" instead of just "/" for the internal directory path (at least on my particular phone).

Brian
 
Upvote 0

brianh

Member
Licensed User
Erel

I want to be able to copy, when needed, a file that contains specific (but different data) from my PC onto my phone and then pick it up in the application. From the link you provided, File.DirInternal / File.DirInternalCache would seem to be the only pre-defined paths for the internal memory but the link also says that these directories are only usable by my own application (so cannot be seen by Windows Explorer when I copy the wanted file to my phone). I'm actually using File.DirInternal for some app configuration data but I need to access other file(s) at run-time.

I cannot use the IDE Files facility as the file will only be created/named/copied some time after I've compiled my app so need some other way of locating the file.

What I think I need is something like File.DirRootExternal but for the internal memory (File.DirRootInternal ??) to which I can then add specific sub-directory paths to where I've copied my file. I don't want to use external memory (ie SD card) as I often switch memory cards.

I accept that using the fuller path I detailed in my reply to Mahares is bad practice but I'm not sure what else I should use.

Although I'm still very much a beginner at B4A, many thanks for an excellent program.

Brian
 
Upvote 0

brianh

Member
Licensed User
Erel
Thanks for the clarification - I did not realise that "storage card" meant on-board memory. I've changed my code to use File.DirRootExternal and it now works OK.
Brian
 
Upvote 0
Top