Save a file in APK and read later?

wdegler

Active Member
Licensed User
Longtime User
At the bottom right of the B4A development screen, I clicked on "Files" (the folder icon), then clicked on the "Add Files" button, then added a file, say, "MyFile", which I then saw listed above.

Next, I successfully compiled the program but as soon as it began to run, it stopped on the 'Initialize' line of the following code:

Dim Fil as RandomAccessFile

If Not(File.Exists(File.DirRootExternal,"MyFile")) then
Fil.Initialize(File.DirAssets,"MyFile"True)
. . .

The error message was
"Last Exception java.io.FileNotFoundException:/AssetsDir/MyFile(No such file or directory)".

I have seen problems like this on other posts but still don't have an answer.
I only need to add a file to the APK for later reading by my app.

Can someone help with this?
 

mc73

Well-Known Member
Licensed User
Longtime User
I think you should copy your file from dir.assets to dir.defaultexternal before using it. Copying should be made after you use your 'if not (file.exists)'.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
As mc73 already suggested you need to copy the file from DirAssets to another directory for example to DirInternal or DirDefaultExternal. In the help file it's written that RandomAccessFile cannot be used with DirAssets.

Best regards.
 
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
Thank you both for your suggestions.

I used your suggestion to replace the 'Initialize' line (and some lines following it) with
File.Copy(File.DirAssets,"MyFile",File.DirDefaultExternal,"MyFile").

I get the same result on the Copy line with the file not found message as before. This seems to mean that, though "MyFile" is in the 'Files' list, that it is yet not in the DirAssets directory.

Suggestions?
 
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
I just noticed something you said in your last post

I am using random access files because they are lists with some of the elements also lists. If DirAssets cannot be used for such files, perhaps I need to create the file in the app if it is not already in the directory I use. If you know a better way, let me know. Perhaps I should do this before I zip a file and send it.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I repeat again, can you post your project as a zip file (IDE menu Files / Export As Zip).
It is much easier to help if we have your code so we see what you have done and how we can test it in the same conditions as you do and therefore give the best advice.

Best regards.
 
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
I appreciate your help, Klaus

I appreciate your help, Klaus. It was useful for me to learn about the limitation of DirAssets and the option to use a file copy function. As my app is fairly large, includes several code modules, and is nearly ready for release, I prefer not to send out the entire source.

My problem was supplying a sample file with the app for the user to see. I have decided to check whether it is already present and if not, to create it in code.

Again, your advice was nonetheless a help.

Walter Degler
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
There is no problem to save a file in the apk (DirAssets).
And then in the code copy it to another folder allowing access.
A code like this should do it:
B4X:
If FirstTime = True And File.Exists(File.DirAssets,"MyFile") And Not(File.Exists(File.DirInternal, "MyFile") Then
  File.Copy(File.DirAssets, "MyFile", File.DirInternal, "MyFile")
End If
You could replace File.DirInternal by File.DirDefaultExternal or File.DirRootExternal or even add a subdirectory.

Best regards.
 
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
Hey, I found the solution!

To Klaus and AC73:

I found the solution! Here is what works:

1. Copy the file directly into the Files folder.
2. Remove the extension from the file name in the Files folder.
3. In code, use the statement
File.Copy(File.DirAssets,"MyFile",File.DirRootExternal,"MyFile" & "MyExt")

This may be useful to others to know.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
To Klaus and AC73:

I found the solution! Here is what works:

1. Copy the file directly into the Files folder.
2. Remove the extension from the file name in the Files folder.
3. In code, use the statement
File.Copy(File.DirAssets,"MyFile",File.DirRootExternal,"MyFile" & "MyExt")

This may be useful to others to know.

I don't get this. Were you copying earlier without the file_extension, and then trying to read the file WITH the extension? This is why Klaus suggested your zip upload. Sometimes, the answer is pretty simply, if code is seen.
 
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
I have a solution!

To mc73 and Klaus:

I have a solution:

This works!
1. Copy the file directly to the Files folder.
2. Remove the extension to the file name in the Files folder.
3. Use the following statement in the code:
File.Copy(File.DirAssets,file name without extension, File.DirRootExternal,file name with extension).

This may be useful to others.

Walter Degler
 
Upvote 0

wdegler

Active Member
Licensed User
Longtime User
What I was doing earlier, MC73

What I was doing earlier was to use the full file name when adding a file to Files and when trying to access it in File.DirAccess. They weren't found that way.

I have found that everything works if my files in DirAcess do not show extensions. When copying them from DirAccess, I use the extension for the destination.

It all works this way.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
There is no reason to remove the extension in DirAssets I do this in several of my programs.
I copy the file in the IDE in the Files Tab with the extension.
And in the code I copy it to another folder still with the extension.
But without seeing your code it's impossible to give concrete answers, we need to try to guess what you could have done wrong.
I must say it's frustrating trying to help without seeing any code, at least a small project showing the problem.
In my post I put "MyFile" without extension according to your post.

Best regards.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@wdegler: When some heavy hitters, with great reputation and knowledge like Klaus and mc73 provide you with help, please follow their instructions. They may get frustrated and become reluctant to help the rest of us who need a lot of help. If you are worried about including your data with your code as you mentioned in your post, you can create a small data sample file with fake records. It can be very helpful.
 
Upvote 0
Top