Storage permissions & API call

The place to discuss anything and everything about running your LiveCode on Android

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Storage permissions & API call

Post by KimD » Fri Apr 03, 2020 12:18 am

Two questions relating to an ongoing battle with the Android downloads folder:

1) If I make the assumption that the downloads folder is located at /storage/emulated/0/download, then my app is currently working on my Android test device; but only if I first go into My Test Device > Settings > Apps > myapp > Permissions > Storage = true on my Android test device. Is there a way to request the this storage permission using LC > Standalone App Settings > Android? I've already got "Write External Storage" ticked, but this doesn't do it.

2) My above assumption isn't always going to be true. From what I've read on stackoverflow, the definitive way to find out the location of the Downloads folder is by using the following Android "api call" - Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS). How do I make an Android API call using LC?

Thanks in advance, and I hope that everyone is staying safe

Kim

LC : Indy 9.5
Test Device : Android 8.1

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Storage permissions & API call

Post by KimD » Fri Apr 03, 2020 4:21 am

Update
I've solved my first problem with - androidRequestPermission "android.permission.WRITE_EXTERNAL_STORAGE"
Still stymied on how to make an Android API call using LC.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Storage permissions & API call

Post by bogs » Fri Apr 03, 2020 9:00 am

Hey Kim,

Congrats on solving the first part of your problem!

I do not develop for mobile, so take the following with a grain of salt!

On the second, I'm not sure there is any built in way.
A lot of the time, it appears the language takes some things for granted, like folder locations for OS'es. If you know how to find out that location yourself from a device, then you *might* be able to leverage that knowledge, but most probably would not even attempt it.

As an example, your particular question came up in a Desktop OS (Linux, in this case). I am sure you know the 'specialFolderPath' function. Well, while it is supposed to be able to return the path to the 'Documents' folder, this will fail if the user renames the folder or uses a different folder or path on 'nix. I haven't tested on Win and Mac, so I can't say there.

To finish out that example, you would have to know which files on 'nix map out those folders your own self to be *positive* you were in what the OS considers documents. A lot of people who program wouldn't know this. While you might be able to find out or figure it out yourself (eventually), some might not even consider it worth the trouble, simply because the end user is less likely to manipulate their environment (unless your target audience is a real bunch of geeks and nerds, who can't keep their fingers out of the tech cookie jar).

I hope that I am wrong in the above, and someone else who dev's using Lc knows the answer to your second question, even though I don't dev for mobile, it is an interesting question just based on it's own merits.
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7230
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Storage permissions & API call

Post by jacque » Fri Apr 03, 2020 6:27 pm

Different manufacturers or hardware will not always have the same path to the external documents folder, so you can't assume or hard code it. Instead, use specialFolderPath("external documents") to get the correct path on any device.

To overcome the permissions issue, open standalone settings to the Android pane, and tick the checkbox that allows external storage. LiveCode will handle the rest and create the proper manifest.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Storage permissions & API call

Post by KimD » Fri Apr 03, 2020 10:39 pm

Thanks Bogs & Jacque

More detail - what I'm trying to achieve is to allow my app to read files that are already in the Android DOWNLOADS folder. I.E. (1) someone sends Android user an email with type XYZ attachment, (2) user opens this email using Gmail, (3) user hits the Gmail download attachment button, (4) user fires up myapp, (5) myapp scans Android default downloads folder, finds the type XYZ file, and opens it.

I've got this working - but only by using :
- androidRequestPermission "android.permission.WRITE_EXTERNAL_STORAGE"; and
- set the defaultFolder to "/mnt/sdcard/Download"

As you've both noted, the problem is that the Android downloads folder can be in different locations on different models of device. "/mnt/sdcard/Download" works for, what I understand to be, the most common scenario; but I'm trying to improve my code so that it will work on a wider range of devices.

I've tested, and:
- LC > Standalone App Settings > Android > Write External Storage - does not, on its own, solve the access permissions problem. I have read a previous post to the effect that you need to additionally reference specialFolderPath("external documents") before your Write External Storage setting will take effect; but
- specialFolderPath("external documents") doesn't seem to help with meeting my goal, as it doesn't resolve to the device's Downloads folder.

I'm hoping that LC will provide a way to retrieve the Android system parameter which stores the file path to the device's downloads folder.
As I understand it, the Android API call "Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)" will do this, but I've got no idea how to make this call using LC.

If this can't be done using LC, then the only other option that I can think of is:
- try and find a list of all of the most common locations that Android devices use for their Downloads folder; and
- have my LC script check whether each of these folders exist on the device; and
- if they do exist, scan them for type XYZ files.
This seems like a variant on Bog's "If you know how to find out that location yourself..." suggestion.

I was also hoping to use this problem as a way to teach myself how to use LC to read Android system parameters (e.g. the location of the downloads folder), but I might just have to give up on that one.

VOTE specialFolderPath("downloads") ;-)

Stay safe

Kim

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7230
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Storage permissions & API call

Post by jacque » Fri Apr 03, 2020 11:09 pm

I haven't actually tested it, but try this:

Code: Select all

put specialFolderPath("external documents") & "/Download" into tPath -- tPath should be what you want
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Storage permissions & API call

Post by KimD » Sat Apr 04, 2020 12:20 am

Hi Jacque

Thanks for trying, but if I try -

put specialFolderPath("external documents") & "/Download" into tPath
set the defaultFolder to tPath

Then the result is "can't open directory"

LC dictionary - 'If you set the defaultFolder to a folder that doesn't exist, the result is set to "can't open directory"'

Kim

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Storage permissions & API call

Post by bogs » Sat Apr 04, 2020 7:29 am

KimD wrote:
Sat Apr 04, 2020 12:20 am
LC dictionary - 'If you set the defaultFolder to a folder that doesn't exist, the result is set to "can't open directory"'
And that is very true, what I find funny about it is, at least where 'nix is concerned, **it still writes a file out, even though it couldn't find the folder :P

On mobile, YMMV of course, but it dumbfounded me.

**Note - in reference to versions I use, the current reality may have changed :P
Image

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Storage permissions & API call

Post by KimD » Mon Apr 06, 2020 2:48 am

Thanks. I think that I've worked out how I can achieve what I want. A Livecode Builder Library utilising the Foreign Function Interface to execute a Java script that will return the file path to the Android Downloads folder. Now all that I need to do is teach myself LCB, the FFI & Java ;-)

trevix
Posts: 960
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Storage permissions & API call

Post by trevix » Sun Feb 26, 2023 10:32 pm

Hello.
I re-enter this conversation because I am trying to access the external folder document (or download, for that matter) on an Android 11 TvBox.
I report here some facts that may be someone is able to explain me.

After having set to true the standalone pane "Write external storage", checked the standlone write permissions on the OS (granted) and put, on launch, a

Code: Select all

androidRequestPermission "android.permission.WRITE_EXTERNAL_STORAGE"
1- specialfolderpath("external documents") returns: "/storage/evulated/0/Android/data/com.trevix.segnapunto/files"
this to me seems to be a path inside the standalone folder, not an external folder as it should, since "com.trevix.segnapunto" is the identifier of the standalone

2- using the app "x-PloreFileManager" I cleared out that the path to the common document folder in my hardware is:
/storage/emulated/0/Documents
3- I noticed that the above app is granted the permission "MANAGE_EXTERNAL_STORAGE" (which is not included in the LC dictionary). I tried to run on my standalone a

Code: Select all

androidRequestPermission "android.permission.MANAGE_EXTERNAL_STORAGE"
but it doesn'work

4- one strange thing is that if I place a file in the common external document folder, using X-PloreFileManager, this line in the standalone correctly report that the result is true:

Code: Select all

there is a file gPref["PathToTheFile"]
where the path is "/storage/emulated/0/Documents/nameOfFile.txt"
Still the function "Files" does not report anything, as well detailed files. And it is not possibile to read the file or write to it.

In my opinion, this all thing of reading/writing to external folders, is a half-done job, that for some reason LC didn't finished. Is it so?

Trevix
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7230
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Storage permissions & API call

Post by jacque » Sun Feb 26, 2023 10:56 pm

LC doesn't have access to that folder. There's a feature request for it:
https://quality.livecode.com/show_bug.cgi?id=23526
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1209
Joined: Thu Apr 11, 2013 11:27 am

Re: Storage permissions & API call

Post by LCMark » Mon Feb 27, 2023 6:32 am

@trevix: Android 11 tightened up access to files which an app doesn't own considerably - an app can no longer (in general) access any file or folder it does not own.
1- specialfolderpath("external documents") returns: "/storage/evulated/0/Android/data/com.trevix.segnapunto/files"
this to me seems to be a path inside the standalone folder, not an external folder as it should, since "com.trevix.segnapunto" is the identifier of the standalone
This is the 'scoped storage' of your app - your app's own (only accessible to it) data folder - hence why it has your apps id in the path.
3- I noticed that the above app is granted the permission "MANAGE_EXTERNAL_STORAGE" (which is not included in the LC dictionary). I tried to run on my standalone a
I think that permission *has* to be in the manifest - you can't dynamically request some permissions - I think that should give you greater access to the device's external (and internal) storage - however it does come with some caveats with regards the Play Store - https://developer.android.com/training/ ... oogle-play.
4- one strange thing is that if I place a file in the common external document folder, using X-PloreFileManager, this line in the standalone correctly report that the result is true:
CODE: SELECT ALL
there is a file gPref["PathToTheFile"]
where the path is "/storage/emulated/0/Documents/nameOfFile.txt"
Still the function "Files" does not report anything, as well detailed files. And it is not possibile to read the file or write to it.
That's just a unix permission thing - you can test existence of a file whether you have access to the folder containing it or the file itself (if you know its name, i.e. full path). In this case you don't have access to the folder containing the file (so cannot list it - i.e. get the files of it), and you don't have access to the file itself so cannot open it for read or write.

For reference, the details of the changes to Android in 11 re storage access are here:
https://developer.android.com/about/ver ... cy/storage

trevix
Posts: 960
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Storage permissions & API call

Post by trevix » Tue Feb 28, 2023 10:14 am

On the Android developer page, I see:
Android 11 (API level 30) further enhances the platform, giving better protection to app and user data on external storage.
What I read is:
We farther restricted user interaction, limiting access to a shared folder, because we don't know how to safely do on mobile what is commonly done on desktop platforms.
And that is true for Apple too...
Said that, I noticed that there are plenty of Android Apps that, given user permission, are able to interact with the documents and download folders.
What is stopping LC on doing this?
Thanks any way
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7230
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Storage permissions & API call

Post by jacque » Tue Feb 28, 2023 6:40 pm

Mark's link to the Google developer page lists the type of apps that can legitimately access the public folders. I suspect that you could do it with permissions from Google's review team if your app fits one of the listed categories. LC would probably have to make some adjustments to the engine too.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Android Deployment”