Android Question Problem with DirRootExternal and DirDefaultExternal

Cesaral

Member
I have done a simple program to test the creation and access to text files, but I am not able to make them work.

Find the test program attached.

I have upgraded B4A to the latest version and it still gives the same errors. The erros I get are:

Error occurred on line: 72 (Main)
java.io.FileNotFoundException: /storage/emulated/0/DirRootExternal.txt: open failed: EPERM (Operation not permitted)
at libcore.io.IoBridge.open(IoBridge.java:492)
at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:449)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)


Error occurred on line: 86 (Main)
java.io.FileNotFoundException: /storage/emulated/0/Android/data/b4a.example/files/DirDefaultExternal.txt: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:492)
at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:449)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)

I would appreciate ANY help that could solve this issue.

Thanks!
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
A bit of source code would be useful. ;)

However, the key lines are:

B4X:
java.io.FileNotFoundException: /storage/emulated/0/DirRootExternal.txt: open failed: EPERM (Operation not permitted)
and
B4X:
java.io.FileNotFoundException: /storage/emulated/0/Android/data/b4a.example/files/DirDefaultExternal.txt: open failed: ENOENT (No such file or directory)

What is the target android are you compiling with? I am assuming it is API 29 or 30.

Then read this:


Then read any of the multiple threads about accessing external files are the latest versions of android.

This is an example of the approved way of accessing external files.
 
Upvote 1

Cesaral

Member
Thanks for your answer!

Attached the source code that I tested. What could I add to the code to make it work?
 

Attachments

  • FileTest.zip
    183.6 KB · Views: 209
Upvote 0

Cesaral

Member
The requirements are simple: My app needs to access the files placed at the File.DirRootExternal file, as many apps out there do. How could I do it?

File.DirRootExternal: It is a portion of the internal device memory referred to as the secondary storage. It is not the REMOVAL SD card. When you update apk, or OS or UNinstall the app, the database will still be there. If you are not concerned about someone manually deleting it, then it is an option. It is always very easy to locate via file manager. Other apps can access it.

If File.DirRootExternal is broken code....Why do we still have it in B4A?

Thanks!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Cesaral

Member
The problem is that I need to append information to a big LOG file....Having to load the entire file to add a register and then save it, is not as optimal as using the Out = File.OpenOutput(File.DirRootExternal, "LogFile", True) function.

So, the specific question is..: What is the best (less resources needed) and fastest method to append a register to a big log file, not having to load it in memory?

Thanks!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This was true in old versions of Android. In newer versions of Android it is difficult to share files like this. Difficult means that you will need to ask the user to explicitly select the folder and the root folder is not accessible at all on Android 12 devices.
The correct way to share files is using FileProvider.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
This was true in old versions of Android. In newer versions of Android it is difficult to share files like this. Difficult means that you will need to ask the user to explicitly select the folder and the root folder is not accessible at all on Android 12 devices.
The correct way to share files is using FileProvider.
I just read the thread about the FileProvider class (which later became a library).
If I understand correctly, thanks to this you will be able to share the files in the DirInternal of your app with other apps. In this way, however, they will still be inaccessible from Windows and I fear that this is no longer possible in any way, right?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
If I understand correctly, thanks to this you will be able to share the files in the DirInternal of your app with other apps. In this way, however, they will still be inaccessible from Windows and I fear that this is no longer possible in any way, right?
If you want your files to be accessible when the device is connected as a USB mass storage device then you can use rp.GetSafeDefaultExternal.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
method to append a register to a big log file, not having to load it in memory?
Open it in append mode.
B4X:
Dim Writer As TextWriter
Writer.Initialize(File.OpenOutput(rp.GetSafeDefaultExternal., "Log.txt", True))
Writer.WriteLine("This is the first line.")
Writer.WriteLine("This is the second line.")
Writer.Close
 
Upvote 0

Cesaral

Member
Where can I find the best place to get information about "rp.GetSafeDefaultExternal" ?

The way the folders are named in Android is crazy. I can´t find a reason for it: You have a folder name when you connect the Android device to the PC/MAC, and the App refers to that folder using another name. I would like to place the log file in /LOG/log1.txt so that the App can fill in this log and the PC/MAC can easily access it

Thanks!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I would like to place the log file in /LOG/log1.txt so that the App can fill in this log and the PC/MAC can easily access it
Only possible with ExternalStorage.

Where can I find the best place to get information about "rp.GetSafeDefaultExternal" ?
And also read: https://www.b4x.com/android/forum/threads/android-jar-targetsdkversion-minsdkversion.87610/#content
 
Upvote 0

Cesaral

Member
Only possible with ExternalStorage.
And also read: https://www.b4x.com/android/forum/threads/android-jar-targetsdkversion-minsdkversion.87610/#content
Thanks Erel, but I have looked for the word GetSafeDefaultExternal in both URLs you gave and I still cant find the information.

Why is this so difficult to find? Is there any reference page in which this term is explained?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top