How to upload images from sdcard to corona sdk

How to display an image from an SD card or phone memory using corona sdk? `

+4
source share
2 answers

I use the Corona SDK for iOS development, but I believe that the corona apps are sandboxed and you can't read anything behind the sandbox. Thought this could only be true for iOS apps.

0
source

You can read and write files to the SD card using the LFS and IO API on Android.

To access the internal memory of the phone, add the android permission "android.permission.WRITE_EXTERNAL_STORAGE" and set the path to "/". From there you can access the memory card.

Example:

local lfs = require("lfs") local path = "/" 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 -- Get the file attributes. local fileAtr = lfs.attributes( path .. "/" .. file ) -- Print path, name and type of file (Directory or file) print(path,file,fileAtr.mode) end end end 

This will print the path, file name and file type in the terminal window. (Tested only on Mac and Android devices.)

I found a way to display the image outside the sandbox on Android and on the simulator. (PC not tested)

Example:

 local lfs = require("lfs") --------------------------- Change this path --------------------------- local path = "path/to/the/image.jpg" -- Change this path to the path of an image on your computer ------------------------------------------------------------------------ local tmpPath = system.pathForFile("tmp.jpg",system.TemporaryDirectory) -- Destination path to the temporary image --------------------------- Read ---------------------------- local file, reason = io.open( path, "r" ) -- Open the image in read mode local contents if file then contents = file:read( "*a" ) -- Read contents io.close( file ) -- Close the file (Important!) else print("Invalid path") return end --------------------------- Write ---------------------------- local file = io.open( tmpPath, "w" ) -- Open the destination path in write mode if file then file:write(contents) -- Writes the contents to a file io.close(file) -- Close the file (Important!) else print("Error") return end ---------------------- Open and remove ----------------------- local img = display.newImage("tmp.jpg",system.TemporaryDirectory) -- Display the image from the temporary directory os.remove(tmpPath) -- Removes the image, so that we can load another image. 
+6
source

Source: https://habr.com/ru/post/1385497/


All Articles