Storage permissions & API call

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

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
KimD
Posts: 211
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: 211
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: 5115
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: 5847
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: 211
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: 5847
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: 211
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: 5115
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: 211
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 ;-)

Post Reply

Return to “Android Deployment”