write files to sd card on android

4 replies [Last post]
stephenj4622a
User offline. Last seen 6 years 2 weeks ago. Offline
Joined: 1 Nov 2011

Hi,

Hope you can help. I can successfully write a file to the android internal memory using

1
2
3
4
5
6
system.pathForFile( "data.txt", system.DocumentsDirectory )
fh, errStr = io.open( path,"a")
if fh
  fh:write(theText .. "\n")
end
io.close()

HOWEVER my question is how do i do the same to the sdcard - i need to write large files and (a) dont want to run out of memory and (b) dont want to have to use ADB pull everytime I want access to the data. There doesnt seem to be an option in pathforfile

thanks

Steve

Replies

jonathanbeebe
User offline. Last seen 5 years 9 weeks ago. Offline
Alumni
Joined: 12 Apr 2011

I believe you can do that by ensuring the following permission is in your build.settings file:

settings = {
    androidPermissions =
    {
        "android.permission.WRITE_EXTERNAL_STORAGE",
    },
}

kam187
User offline. Last seen 3 years 21 weeks ago. Offline
Joined: 10 Sep 2010

Hmm, how do you reference the SD card when reading/writing tho?

stephenj4622a
User offline. Last seen 6 years 2 weeks ago. Offline
Joined: 1 Nov 2011

thanks, but how do you reference the SD card?

system.pathForFile( "data.txt", system.DocumentsDirectory )

doesnt allow this?

thanks

teufelss
User offline. Last seen 1 year 13 weeks ago. Offline
Joined: 9 Feb 2012

If anyone read this ... i was able to make it work writing the full path
enable the external_write permission on the build.settings with

"android.permission.WRITE_EXTERNAL_STORAGE",

then

local lfile = io.open("/mnt/local/test.txt","w")
lfile:write("test")
lfile:close()

to a sd card the path would be /mnt/sdcard/test.txt at least on my device

hope it helps someone ...

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.