Creating a file on external storage

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
okk
Posts: 178
Joined: Wed Feb 04, 2015 11:37 am

Creating a file on external storage

Post by okk » Sat Feb 27, 2016 9:39 pm

Hi,
I want to write application data to a directory on external storage so that it is accessible with Android File Transfer or a similar file browser. Therefor the often mentioned specialFolderPath("Documents") solution is not working for me. What puzzles me is the following:

Code: Select all

put field "data" into URL "file:/mnt/sdcard/Android/data/com.apocalypse.gpstest/data.txt"
actually writes application data into the data.txt file when the file already exists; in my case I copied it from my macbook into the com.apocalypse.gpstest folder using Android File Transfer. But the code doesn't have any effect if the data.txt file doesn't exist yet. So, I can write to an existing file, but I cannot create that file. I am using Android 4.2.2 with LC 8 DP 15.

Thanks for any insights
Oliver

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Creating a file on external storage

Post by jacque » Sun Feb 28, 2016 8:02 pm

Did you select the option for external storage in the Android pane of the standalone settings? I haven't ever needed to create files there but I'm guessing that would be required.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

okk
Posts: 178
Joined: Wed Feb 04, 2015 11:37 am

Re: Creating a file on external storage

Post by okk » Mon Feb 29, 2016 7:55 am

Hi,
I have checked "Write External Storage" in the Android standalone setting pane. The curious thing is that I can write to the existing document, so an incorrect filepath cannot be the issue, but I just cannot create the document from within my application. Could you perhaps try if you can create a file somewhere on the external storage. Thanks.
Oliver

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Creating a file on external storage

Post by jacque » Mon Feb 29, 2016 7:13 pm

I think you've hit an OS restriction. Strong security measures were put in place on kitkat, weakened slightly in lollipop, and completely changed in marshmallow. Samsung implemented their own variations on the rules in some versions of the OS. So you'll get different permissions on different phones depending on several factors.

Android uses the same type of sandboxing as IOS where you can only write to folders owned by the app. See Google:
https://www.google.com/search?q=can+t+w ... &tbs=qdr:y
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

okk
Posts: 178
Joined: Wed Feb 04, 2015 11:37 am

Re: Creating a file on external storage

Post by okk » Tue Mar 01, 2016 6:15 pm

Hi and thanks for the advice. In the moment I cannot test with anything other than Android 4.2. What I actually want to achieve to have a small app that can download large audiofiles (500MB and more) and store those audiofiles on external storage. What is important is that those audio-files are not deleted when I install a new version of the software. I also want that a second application can access those same files.

So how can I make the app "own" folders on external strorage? I thought that the "Write External Storage" is actually exactly about this?

Thanks!
Oliver

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Creating a file on external storage

Post by SparkOut » Tue Mar 01, 2016 8:33 pm

Each app has its own location, which may or may not be on external storage. Each app has a folder in the sort of path you attempted. This is aliased by specialFolderPath("resources ") -or engine - which is read only, and specialFolderPath(" documents ") which is writable, but only by the parent app.
For writable paths of external storage that can be accessed by different apps, MaxV is the go to guy on Android.
http://forums.livecode.com/viewtopic.ph ... 15#p127466

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Creating a file on external storage

Post by jacque » Tue Mar 01, 2016 8:47 pm

What is important is that those audio-files are not deleted when I install a new version of the software. I also want that a second application can access those same files.
Regardless of where the files are written, Android will never remove the app data files when the app is updated.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

okk
Posts: 178
Joined: Wed Feb 04, 2015 11:37 am

Re: Creating a file on external storage

Post by okk » Wed Mar 02, 2016 12:40 am

Hi,
my reference is this info on the android developer pages. http://developer.android.com/training/b ... files.html
The relevant section is >Save a File on External Storage< in the second part of the page. They talk about Public files and Private files saved on external storage. Reading it I was wondering if Livecode would require more Android specific specialFolderPath arguments, e.g. specialFolderPath(" external documents ") that would point to app specific private files on the external storage. Or specialFolderPath(" external public ") that would emulate the getExternalStoragePublicDirectory() method. I am still on Android 4.2. so before the stricter regime with KitKat therefore doubtful that this is a sandboxing issue.

Best
Oliver

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Creating a file on external storage

Post by jacque » Wed Mar 02, 2016 7:09 pm

I don't know the answer, but requesting another specialFolderPath would be a good feature request in the QCC, and if there's an existing way to do it they'll tell you.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

okk
Posts: 178
Joined: Wed Feb 04, 2015 11:37 am

Re: Creating a file on external storage

Post by okk » Mon Mar 07, 2016 10:57 am

Hi,
I still tested a bit more and I find out that the issue seems to be with Android File Transfer. The line of code posted in my first post was actually creating a new file "data.txt" on external storage, but Android File Transfer just was not displaying the file. So the file was created but stayed somehow invisible. In any case, the idea with default folder paths to external storage seems reasonable in my opinion. I never feel comfortable to hard-code file paths to an app.

Best
Oliver

trevix
Posts: 1079
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Creating a file on external storage

Post by trevix » Sun Aug 18, 2024 6:11 pm

Just so that you know:

I was looking for a way, on Android 11, to store outside of the sandbox an UID that wouldn't change when reinstalling my App.
No matter what, I could not write or read a text file in the Document/Dowload/or else folder.
I could just find out if the file existed.

Then, I gave a try with Folders.
So much for the sandbox: I can create/delete and read name of folders in the Document folder (to understand...of Android).
Without any special permission, now I can save my UID string as the name of a created folder, and be sure it want change when deleting the App.
Practically an ID of the device.
Haven't tried yet with Android >13 but it sound promising.

Any comment on this?
Trevix
Last edited by trevix on Sun Aug 18, 2024 6:17 pm, edited 1 time in total.
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Creating a file on external storage

Post by Klaus » Sun Aug 18, 2024 6:16 pm

Yep, a long standing issue/feature request: https://quality.livecode.com/show_bug.cgi?id=23526

trevix
Posts: 1079
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Creating a file on external storage

Post by trevix » Thu Aug 22, 2024 5:59 pm

Just so that you know:

I discovered that while it is possible to create a folder on the "Documents" folder (of the OS) on Android 13, the same is not possible on Android 10.
This, more then an Android reverse on security, to me it sounds like a Livecode problem.

Any idea?

Thanks
Trevix
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

Post Reply