Android Question Can't write/read text file

rfresh

Well-Known Member
Licensed User
Longtime User
I have the following code in my Create section:

B4X:
If File.Exists(File.DirDefaultExternal, "EZHolds.txt") = False Then
   File.Copy(File.DirAssets, "EZHolds.txt", File.DirDefaultExternal, "EZHolds.txt")
End If

However, when the above code block executes, I get this error:
java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.rfresh737.ezholds/files/EZHolds.txt (No such file or directory)

Using file manager I look at my SD card but do not see the com.rfresh737.ezholds folder. I have it defined in as my Package name in my Build Configuration.

I have the file in my IDE Files list and I have an SD card.

Thanks for any help...
 

ilan

Expert
Licensed User
Longtime User
I have the following code in my Create section:

B4X:
If File.Exists(File.DirDefaultExternal, "EZHolds.txt") = False Then
   File.Copy(File.DirAssets, "EZHolds.txt", File.DirDefaultExternal, "EZHolds.txt")
End If

However, when the above code block executes, I get this error:
java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.rfresh737.ezholds/files/EZHolds.txt (No such file or directory)

Using file manager I look at my SD card but do not see the com.rfresh737.ezholds folder. I have it defined in as my Package name in my Build Configuration.

I have the file in my IDE Files list and I have an SD card.

Thanks for any help...

what is your TargetSDK in Manifest?
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
26

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.rfresh737.ezholds/files/EZHolds.txt (No such file or directory)
You need the RunTimePermission library checked. Use the below code. Please note that the file will be saved in the external portion of the Internal storage. It is not on the removable SD card, it is here: Android/data/com.rfresh737.ezholds/files
B4X:
Dim rp As RuntimePermissions  'needs runtimePermission lib checked
Dim MySafeFolder As String=rp.GetSafeDirDefaultExternal("")   
    If File.Exists(MySafeFolder, "EZHolds.txt") = False Then
        File.Copy(File.DirAssets, "EZHolds.txt", MySafeFolder, "EZHolds.txt")
    End If
Welcome back @rfresh . We have not heard from you in years.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Thank you. That worked as you stated.

However, I'm confused about this line:

B4X:
Dim MySafeFolder As String=rp.GetSafeDirDefaultExternal("")

It says DefaultExternal so why is the file found in Internal Storage? I'm fine with the file being stored in Internal Storage as the data is very little but just curious.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I watched your Runtime Permissions tutorial. I decided to use the File.DirInternal location to save my data file to.

However, this code doesn't copy the file when the file does not exist in the Internal storage location. I checked the location using a file manager and it's not there. I checked the DirAssets location and the file is there ready to be copied but the IF block registers as True for some reason.

B4X:
If File.Exists(File.DirInternal, "EZHolds.txt") = False Then
    File.Copy(File.DirAssets, "EZHolds.txt", File.DirInternal, "EZHolds.txt")
End If
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I checked the location using a file manager and it's not there
You cannot access File.DirInternal from any other program or explorer.

If it didn't throw an error then it worked.

I decided to use the File.DirInternal location to save my data file to.
That's the correct solution if you don't need to share the file with other apps.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I believe the cause of your error is much simpler: You are using capitals in a file name that is saved in DirAssets:
"EZHolds.txt". Change it to "ezholds.txt" and your problems will be over (I hope...).
I am afraid that is not correct:
Here is the way Assets folder works: Suppose you have a file with uppercase name: MAHARES.txt somewhere in one of you folders.
When you add if via IDE file manager to files folder, the name is automatically converted to all lowercase.
At run time even if you type it in your code as uppercase or mix of cases, the compiler changes it to all lower case.
In other words, if you show it in your code as MaHaRes.Txt, it will still be found.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
ell, I never bother to add any files with the Files Manager, mainly because B4A automatically includes all files that are in the project's Files folder.
Just click on the Sync button and the files listed in the Files tab will be updated based on the Files folder content (and the cases will be set correctly).
 
Upvote 0
Top