DS paths

141 views
Skip to first unread message

Alan Hendry

unread,
Feb 2, 2022, 6:51:09 AM2/2/22
to DroidScript
Hi,
There's a lot of posts about problems understanding the paths for files in DS
So I want to put some notes here, probably get corrected, and possibly add to the wiki.

Probably the simplest path is Relative (it doesn't start with /) and refers to files in your app, folders in your app (usually Img, Snd, Misc, HTML) and the files in those folders.

Next are paths supplied by DS Methods that you can write to and read. 
The most useful are probably
app.GetPrivateFolder(...)  - User can't see folder/files
app.GetSpecialFolder(...) - User can see files

More complicated is if you specify an absolute path (starts with /) 
First, paths are automatically mapped from what you specify in DS to the underlying Android system. 
Second, the mapping is different for when you are running in an IDE (DS app Home page, DS app edit page, or WiFi IDE) versus running from an installed APK/AAB.
Third, the mapping is different for Android 9 (and lower) compared to Android 10 (and higher).

Also Android 10 (and higher) scoped storage can restrict the folders you can read/write.

A good way to see how your absolute path is being mapped (and check if scoped storage is restricting it)  is to write a small app to write a file with a distinctive name then find it with your Files app. (Scoped Storage may restrict the folders that your Files app can see, 
so I use X-Plore a free app from the Google Play Store.)

If you want to use the external SD card then see Sample (touch Rocket in top-right of DS)
Sdcard acess- writing files to the external sd card

Some "external" functions (notably nodejs packages) 
need "short paths" to be converted to "long paths" using app.RealPath(...)

Regards, ah

Steve Garman

unread,
Feb 2, 2022, 7:50:16 AM2/2/22
to DroidScript
I that's a good start Alan. There's very little I would disagree with

Last time I looked at app.GetSpecialFolder() it was a very poor friend if scoped storage might come into play. Especially in an APK where you can't predict what the environment would be

For media files (and maybe some others) it would be worth mentioning content URIs and therefore
app.Path2Uri() and app.Uri2Path()

Also, I would suggest pointing out that
  app.Alert(app.RealPath(mypath),mypath)
is a good way of showing exactly where the file is that mypath is pointing to

But I wouldn't quarrel with you if you wanted to put what you have written on the wiki as it is

Also I am rather hoping the next release of DroidScript will further complicate things but that's a story for another day

Alan Hendry

unread,
Feb 2, 2022, 9:43:20 AM2/2/22
to DroidScript
HI,
Looking back at my code for using exif nodejs I actually had to RealPath and then Path2Uri.
The Sample Media Store returns addresses of the form content://media, 
and the Example for QueryContent has content://
not sure if they're Uri's (and if they unnecessarily complicate things).
I recall a post that to get videos to play they had to be extracted from inside the app (ExtractAssets).
In my head and in my notes app.GetSpecialFolder("Pictures")  and  app.GetSpecialFolder("Downloads")
worked in SPK/APK Oreo and 11, I can retest. 
And PrivateFolder worked except for option "external".
Regards, ah

Steve Garman

unread,
Feb 2, 2022, 1:41:52 PM2/2/22
to DroidScript
No, it looks like you're right
app.GetSpecialFolder("Pictures") now returns athe right canonical path

That was one that used to annoy me

Dave

unread,
Feb 2, 2022, 3:48:28 PM2/2/22
to DroidScript
Probably best to hang fire on the Wiki as storage paths generally are about to be enhanced in the next release - 

/Storage/* -> /Internal storage/Android/data/com.myapp.demo/files/* for scoped, or /Internal storage/* for non-scoped)

/Internal/* -> /Internal storage/*

/External/* -> /SDCARD/*

Note: /Internal and /External will need to prompt the user for permissions (uses SAF under the hood)

Symbroson

unread,
Feb 2, 2022, 3:55:48 PM2/2/22
to DroidScript
I'll add a docs page to the intro about this if required
Currently the docs only distinct absolute and relative paths when a path type is used
It'll probably better to use a generalized path type pointing to this introduction page from now on as this is affecting all path arguments is that correct?

Alan Hendry

unread,
Feb 2, 2022, 4:52:47 PM2/2/22
to DroidScript
Hi,

Slight clarification, 
it was GetPrivateFolder("Pictures","external") that my code couldn't write to
Today I realized I hadn't tried GetPrivateFolder("","external") it seems to be accessible in and SPK on Oreo
Should be able to test APK and 11.

Frequently paths contain com.smartphoneremote.androidscript, is the correct  term "package-name"?
And am I right in thinking that when using the builder the first input field is also the package-name, which is then used in paths when the app is installed?

Yes! app.Alert(app.RealPath(mypath),mypath) is a v. good way of understanding where files will go.
To check whether you can actually write to the folders I wrote a small test app a while ago
which is why I mentioned GetPrivateFolder and GetSpecialFolder 
(I couldn't write to GetInternal, GetExternal, GetPath, GetAppPath, Relative)
but it may be worth users doing their own testing for their specific paths (including Android 12).

Regards, ah

FrickelPit

unread,
Feb 3, 2022, 3:47:43 AM2/3/22
to DroidScript
As i pointed out earlier (https://groups.google.com/g/androidscript/c/65fEGIMuUVo) Path2Uri doesn't work on my Sony device with Android 11. All of the following statements return an empty string and "WARNING: App.Path2Uri() failed! (Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference)" in debug window:
        txt = app.CreateText(app.Path2Uri(app.GetPrivateFolder("test")))
        txt = app.CreateText(app.Path2Uri(app.GetSpecialFolder("Downloads")))
        txt = app.CreateText(app.Path2Uri(app.RealPath(app.GetSpecialFolder("Pictures"))))
        txt = app.CreateText(app.Path2Uri("/sdcard/"))
        txt = app.CreateText(app.Path2Uri("sdcard/"))
Has anyone else tested it or is there a misunderstanding of Path2Uri on my side?
Peter

Steve Garman

unread,
Feb 3, 2022, 4:23:19 AM2/3/22
to DroidScript
app.Path2Uri only only works on a path to a file, not a path to a folder

Alan Hendry

unread,
Feb 3, 2022, 5:48:37 AM2/3/22
to DroidScript
HI,
I think the problem with Path2Uri is a difference in terminology/definitions.
In microsoft windows path usually refers to a folder (e.g. C:\windows\system32)
(or the environment variable PATH which is a list of folders to search)
But in DroidScript it means the folder and file name (e.g. /Storage/test.txt)
Regards, ah
Reply all
Reply to author
Forward
0 new messages