Storage Access Sample fails

653 views
Skip to first unread message

mAndroid

unread,
Aug 19, 2023, 7:15:22 AM8/19/23
to DroidScript
Hi,

I started to use the droidscript more intensive after several year subscription.
But I have got a trouble.
Galaxy8
 I got error with access to files on external drive, buit I have received the following error message:
WARNING: App.ReadFile() failed! (Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord{6321f66d0 26744:com.smartphoneremote.androidscriptfree:NewActivityProcess/u0a216} (pid=26744, uid=10216) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs)
Do I have chance to servive this?
I am too beginner such an error.
Thank you in advance.

Ferenc

coaxc...@gmail.com

unread,
Aug 25, 2023, 2:36:55 AM8/25/23
to DroidScript
Hi, did anyone manage to find a resolution here

Symbroson

unread,
Aug 26, 2023, 8:11:13 AM8/26/23
to DroidScript
this is probably a scoped storage issue. search for "scoped storage" in the forum here if you need more information
Basically make sure to use /Storage as path prefix. If you need access to /sdcard you will need to request user permissions for that and use the Storage Access Framework.
SAF is available since version 2.51. This was posted by Dave in the beta release:

2. More support for SAF (storage access framework) which allows you to read and write and list files in folders outside of your app's scoped storage sandbox (including the external flash cards)

There are now 3 special keywords which can be used at the front of file paths to indicated that you want to access the internal sdcard, the external sdcard or the app's default storage location -
/Internal/...
/External/...
/Storage/...

These keywords should be used from now on in preference to '/sdcard/..' style paths (note that they are case sensitive).
You must ask for permission from the user with the app.GetPermission() method if you want to read or write files outside of your app's sandbox area on Android 10 or higher (scoped storage)
Please check out the new 'Storage Access' sample to see how to use these new SAF features.

Moreno B

unread,
Aug 26, 2023, 1:24:22 PM8/26/23
to DroidScript
I'm trying to save a txt file using the function app.WriteFile() and its method "Append".
If I use the special keyword /Storage/, the append method works as expected.
Instead if I use the keyword /Internal/, the append method doesn't work.

The following is the code I use for looping all the strings I nedd to append:
for(let i=0;i<txtToExport.length;i++)
    {
        app.WriteFile( myFullPath, "title: "+txtToExport[i].title+"\n", "Append")
        app.WriteFile( myFullPath, "user: "+txtToExport[i].body_user+"\n", "Append")
        app.WriteFile( myFullPath, "password: "+txtToExport[i].body_pwd+"\n", "Append")
        app.WriteFile( myFullPath, "website: "+txtToExport[i].body_web+"\n", "Append")
        app.WriteFile( myFullPath, "\n", "Append")
    }

In the case of /Internal/ keyword the file contains only the last string of the loop.

Symbroson

unread,
Aug 27, 2023, 6:20:46 AM8/27/23
to DroidScript
This is definitely a bug. You can use following workarounds until this is resolved:
Read the file into memory, append your data and write it back as a whole
Or copy the file over to internal storage where append works, and copy it back when youre done.

Moreno B

unread,
Aug 27, 2023, 1:11:35 PM8/27/23
to DroidScript
Thank you very much for the help. 
I will try both of your options and let you know.

mAndroid

unread,
Aug 28, 2023, 1:18:31 PM8/28/23
to DroidScript
Hi,

Thank you to suggest a way to solve the problem. I am try with that scoped strorage stuff.
I come back later..

Ferenc

Moreno B

unread,
Aug 29, 2023, 1:23:14 PM8/29/23
to DroidScript
@Symbroson, before trying the methods you suggested I tried another one which is ok for me.
I changed the above code and, instead of looping through all strings and save them one by one, I join all of them like this before saving:

myFullString=""

    for(let i=0;i<txtToExport.length;i++)
    {
        myFullString=myFullString+"title: "+txtToExport[i].title+"\n"
        myFullString=myFullString+"user: "+txtToExport[i].body_user+"\n"
        myFullString=myFullString+"password: "+txtToExport[i].body_pwd+"\n"
        myFullString=myFullString+"website: "+txtToExport[i].body_web+"\n"
        myFullString=myFullString+"\n"
    }
    app.WriteFile( myFullPath2,myFullString)

This way I only use the app.WriteFile() function once.
It seems all ok since all the strings are stored.

Symbroson

unread,
Aug 29, 2023, 1:54:47 PM8/29/23
to DroidScript
Of course this is perfectly fine as well. If you deal with large files (over 10 MB) it is worth thinking about more refined file operations as reading and writing the whole file repeatedly is very slow

mAndroid

unread,
Aug 30, 2023, 3:53:16 PM8/30/23
to DroidScript
Hi,

I spent some time with this because I needed to "absorb" the information. :-)
So first of all the suggested Storage Access sample gives me that error when I try to use the EXTERNAL OPTION into the drop down. (Internal option works well.)
So unfortunatelly I cannot use that sample as a GOOD EXAMPLE... :-(

the funny is that I cannot receive any message about the missing permission even if the app contains permission check.
.. and I received thet alert write failed..... :-)

Do you have any idea?


Ferenc

Dave

unread,
Oct 13, 2023, 8:51:49 AM10/13/23
to DroidScript
@mAndroid:  The latest beta DS 2.64b2 (coming soon) adds support for writing/reading from an external USB drives on scoped storage devices like this - 

 var fldr = "/USB/MyData"

 if( !app.CheckPermission( fldr ) )
    {
        alert( "Please give permission to access the " + fldr + " folder or a parent folder" )
        app.GetPermission( "usb" )
        return
    }
   
    app.MakeFolder( fldr )
    app.WriteFile( fldr+"/file.txt",  "Hello" )

It remembers the permissions for the drive between unplugging and plugging too :)

Dave

unread,
Oct 13, 2023, 8:54:50 AM10/13/23
to DroidScript
Might add /USB1, /USB2, /USB3 etc in future if people need multiple drive access....and/or maybe access drive by name.
Reply all
Reply to author
Forward
0 new messages