Android Question File.DirRootExternal in Sdkversion 30?

Tjitte Dijkstra

Member
Licensed User
Longtime User
After upgrading to B4a v 11.2 I got the recommendation to change the sdkversion in the Manifest like this: android:targetSdkVersion="30"
I compiled my app and all worked fine.
If File.DirRootexternal was called from the main module it worked, but if File.DirRootexternal was called from a sub-module I got a fatal error: EACCESS(Permission denied)
What to add to the sub modules, or what else?
(If I downgrade to sdkversion 28 the problem is solved as well....)
 

agraham

Expert
Licensed User
Longtime User
If File.DirRootexternal was called from the main module it worked, but if File.DirRootexternal was called from a sub-module I got a fatal error: EACCESS(Permission denied)
This is not expected behaviour, it should either work or not regardless of the location of the call. There is probably some other difference causing this. What version of Android are you running the app on? Can you post a small project showing the problem?
 
Upvote 0

Tjitte Dijkstra

Member
Licensed User
Longtime User
My tablet runs Android v. 11
A small project? The app in total is > 3000 lines of code.
In the main module this works:
If (extern = 1) And (File.Exists(File.DirRootExternal,"ATLAS10/MAP1.jpg")) Then Button1.SetBackgroundImage(LoadBitmap(File.DirRootExternal,"ATLAS10/MAP1.jpg"))

In a sub module this does not work:
If File.Exists(File.DirRootExternal,"ATLAS10/"&Bestand) Then
TextReader1.Initialize(File.OpenInput(File.DirRootExternal, "ATLAS10/"&Bestand))
Else

N.B. File.Exists works. Bestand is the name of the file
The error is in the line TextReader1.Initialize(File.OpenInput(File.DirRootExternal, "ATLAS10/"&Bestand))
 
Upvote 0

MikeSW17

Active Member
Licensed User
Longtime User
Are you certain the variable 'Bestand' is set correctly?
File.Exists may return true if the folder ATLAS10 exists and Bestand is Empty/null
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
A small project? The app in total is > 3000 lines of code.
I didn't say your project was small, I said "Can you post a small project showing the problem". It is standard practice to try to recreate the problem in a small project as it makes it easier to find the problem. You will frequently see Erel request this in response to a problem for this reason. In addition cutting a large project down often by itself will show you the reason for the problem.

Cardinal rule number 1. Don't compare apples and oranges. You are not trying to access the same file in both places. One appears to be an image and the other a text file.
 
Upvote 0

Tjitte Dijkstra

Member
Licensed User
Longtime User
The point is: if I recreate the problem in a small project, there is no error.

If I change the sdkversion 30 to sdkversion 28 there is no problem either!
If I change the sequenze (*.jpg first and *.txt second), the error comes with the *.jpg as well.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
The point is: if I recreate the problem in a small project, there is no error.
Why don't you include the small project you are talikng about, why don't you post it. You said there is no error, so it means you made a small project.
A Device with OS 11 and your project has a target SDK of 30 and you are trying to use FIle.DirExternal. If it worked for you, it is a miracle, because it does not
 
Upvote 0

Tjitte Dijkstra

Member
Licensed User
Longtime User
Why don't you include the small project you are talikng about, why don't you post it. You said there is no error, so it means you made a small project.
A Device with OS 11 and your project has a target SDK of 30 and you are trying to use FIle.DirExternal. If it worked for you, it is a miracle, because it does not
Mahares,
What shoul I use in stead of File.DirRootExternal?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
What shoul I use in stead of File.DirRootExternal?
If you want your project to run without any issues and no permission required, use File.DirInternal to store and access your files (also know as xui.DefaultFolder if you use crossplatform, but it appears your are not for now)
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Tjitte,

What agraham is saying is that if "File.DirRootexternal" works in one spot in your app while using targetSdkVersion="30", then it should work in other spots of your app as well. So, if is does work in one spot, but fails in another spot, then that just means the problem is NOT with File.DirRootexternal.

So, there is something else going on that is preventing File.DirRootexternal from working in that other spot.

So, you basically need to do trial and error to make your app smaller and smaller until it starts to work in both spots, then you should discover what you last changed that allowed it to work.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Tjitte,

What agraham is saying is that if "File.DirRootexternal" works in one spot in your app while using targetSdkVersion="30", then it should work in other spots of your app as well. So, if is does work in one spot, but fails in another spot, then that just means the problem is NOT with File.DirRootexternal.

So, there is something else going on that is preventing File.DirRootexternal from working in that other spot.

So, you basically need to do trial and error to make your app smaller and smaller until it starts to work in both spots, then you should discover what you last changed that allowed it to work.
Errr - aren't there restrictions on using DirRootExternal from Android 11 on? You should probably take a look at this: https://developer.android.com/training/data-storage/use-cases#running_on

- Colin.
 
Upvote 0

Tjitte Dijkstra

Member
Licensed User
Longtime User
Flow chart to help choose folder

Forget from accessing File.DirRootExternal directly. It is no longer possible.
So the only possibility is File.DirInternal? But in my app users have the possibility to write short pieces of information (some *.txt and a *.jpg) on a laptop or chromebook and then copy these files to the map ATLAS10 on the tablet. They love to do that and my app (a topography-trainer) is more or less a individualized scrapbook. It is not longer possible in Android 11?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
So the only possibility is File.DirInternal?
No, there are some alternatives but they will usually require user intervention.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
No, there are some alternatives but they will usually require user intervention.
Andrew. I don't think his app is in App Store. Why didn't you recommend your class. It works using File.DirExternal directly after getting the permission.
 
Upvote 0

Tjitte Dijkstra

Member
Licensed User
Longtime User
@Mahares: what do you mean by "recommend your class?" Should that open the possibility of using the dir ATLAS10?
BTW:
1. if I change the sdk to 28 there is no problem.
2. the app is in the App Store.
 
Upvote 0

Tjitte Dijkstra

Member
Licensed User
Longtime User
My source code untill now
in the section Process_Globals:
Dim rp As RuntimePermissions

In the Sub_ActivityCreate:

rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
rp.CheckAndRequest(rp.GetSafeDirDefaultExternal("ATLAS10"))

What else to realize the option for users to add their own information to my topography app?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
what do you mean by "recommend your class?"
In the link I gave you from aGraham class, he has the class module and an example using the class. Look and run at how the example. It uses the MES (Manage External Storage) and see if you can apply ot to your project. If you get stuck post code or preferably a small project if you can. I used the class in my project to open and access data in a SQLite database in File.DirRootExternal after I was granted the permission and was able to run queries, etc.
 
Upvote 0
Top