write files to sd card on android

Hi,

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

[lua]system.pathForFile( “data.txt”, system.DocumentsDirectory )
fh, errStr = io.open( path,“a”)
if fh
fh:write(theText … “\n”)
end
io.close()[/lua]

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 [import]uid: 101461 topic_id: 17237 reply_id: 317237[/import]

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

settings = { androidPermissions = { "android.permission.WRITE_EXTERNAL_STORAGE", },}[/code] [import]uid: 52430 topic_id: 17237 reply_id: 65173[/import]

Hmm, how do you reference the SD card when reading/writing tho? [import]uid: 8872 topic_id: 17237 reply_id: 65672[/import]

thanks, but how do you reference the SD card?

system.pathForFile( “data.txt”, system.DocumentsDirectory )
doesnt allow this?

thanks

[import]uid: 101461 topic_id: 17237 reply_id: 65747[/import]

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 … [import]uid: 125477 topic_id: 17237 reply_id: 144615[/import]

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 … [import]uid: 125477 topic_id: 17237 reply_id: 144615[/import]

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 … [import]uid: 125477 topic_id: 17237 reply_id: 144615[/import]

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 … [import]uid: 125477 topic_id: 17237 reply_id: 144615[/import]

any news about the correct path to the sdcard

it seems different on several devices

thanks

chris

any news about the correct path to the sdcard

it seems different on several devices

thanks

chris

I know it’s bad to necro threads, but just to help someone having the same problem, turns out the path for mine was /mnt/extSdCard

You can check the files and directory names using this snippet though, I hope it helps someone!

local lfs = require(“lfs”)

local function testPath()

local path = “/mnt/extSdCard” – using only “/” will printout all the files and directories, so I used /mnt and found extSdCard

local pathType = “”

– Check to see if path exists

if path and lfs.attributes( path ) then

   pathType = lfs.attributes( path ).mode

end

– Check if path is a directory

if pathType == “directory” then

  for file in lfs.dir( path ) do

      if “.” ~= file and “…” ~= file then

      print("FILE: " … file)

         – Get the file attributes.

         local fileAtr = lfs.attributes( path … “/” … file )

         – Print path, name and type of file (Directory or file)

         if fileAtr ~= nil then print(path,file,fileAtr.mode) end

      end

   end

end

end

testPath()

I know it’s bad to necro threads, but just to help someone having the same problem, turns out the path for mine was /mnt/extSdCard

You can check the files and directory names using this snippet though, I hope it helps someone!

local lfs = require(“lfs”)

local function testPath()

local path = “/mnt/extSdCard” – using only “/” will printout all the files and directories, so I used /mnt and found extSdCard

local pathType = “”

– Check to see if path exists

if path and lfs.attributes( path ) then

   pathType = lfs.attributes( path ).mode

end

– Check if path is a directory

if pathType == “directory” then

  for file in lfs.dir( path ) do

      if “.” ~= file and “…” ~= file then

      print("FILE: " … file)

         – Get the file attributes.

         local fileAtr = lfs.attributes( path … “/” … file )

         – Print path, name and type of file (Directory or file)

         if fileAtr ~= nil then print(path,file,fileAtr.mode) end

      end

   end

end

end

testPath()