Q: How to add a image to the system.DocumentsDirectory?

Hello guys,

I am curious if the only way to add an image to the directory: system.DocumentsDirectory is to download the image first (or doing a display.save)? OR is there a function that could take an image from an asset folder (same place a main.lua) and place it there?

Thanks if anybody know.

Mo [import]uid: 100814 topic_id: 33545 reply_id: 333545[/import]

You could use regular file IO to copy the file. Just make sure when you open the file for read and the other file for write that you do it in binary mode.

[import]uid: 199310 topic_id: 33545 reply_id: 133312[/import]

Cool! Something like this?

–[lua]local pathRead = system.pathForFile( “screenShot1.jpg”,system.ResourceDirectory )
local file = io.open( pathRead, “r” )
local imageData = file:read( “*a” )
io.close( file )
local myImage

local pathWrite = system.pathForFile( “image.jpg”, system.DocumentsDirectory )
local file = io.open( pathWrite, “w” )
file:write( myImage )
io.close( file )
file = nil[/lua]

My guess this will open the screenShot image in the directory where main.lua reside (and the screenshot…not really a screenshot but any image put in there…marketing…) and transfer it into the system.DocumentsDirectory as myImage

Would this make sense to you? I have to admit I did not work with file I/O so it is all very confusing still. I found this great info here:

http://www.coronalabs.com/blog/2012/02/14/reading-and-writing-files-in-corona/

Thanks again.

Mo

EDIT: I found exactly what I was looking for here BUT I am not sure if the “bug” people were talking about in that page is still there. I will investigate but the function looks very good and simple enough!

copyFile( “Icon.png”, system.ResourceDirectory, “NewIcon.png”, system.DocumentsDirectory )

[import]uid: 100814 topic_id: 33545 reply_id: 133321[/import]

Try this.

local pathRead = system.pathForFile( "screenShot1.jpg",system.ResourceDirectory )  
local file = io.open( pathRead, "rb" ) --local imageData = file:read( "\*a" )  
io.close( file )  
   
local pathWrite = system.pathForFile( "image.jpg", system.DocumentsDirectory )  
local file = io.open( pathWrite, "wb" ) --file:write( imageData ) --io.close( file )  
file = nil  

[import]uid: 199310 topic_id: 33545 reply_id: 133354[/import]

You could use regular file IO to copy the file. Just make sure when you open the file for read and the other file for write that you do it in binary mode.

[import]uid: 199310 topic_id: 33545 reply_id: 133312[/import]

Cool! Something like this?

–[lua]local pathRead = system.pathForFile( “screenShot1.jpg”,system.ResourceDirectory )
local file = io.open( pathRead, “r” )
local imageData = file:read( “*a” )
io.close( file )
local myImage

local pathWrite = system.pathForFile( “image.jpg”, system.DocumentsDirectory )
local file = io.open( pathWrite, “w” )
file:write( myImage )
io.close( file )
file = nil[/lua]

My guess this will open the screenShot image in the directory where main.lua reside (and the screenshot…not really a screenshot but any image put in there…marketing…) and transfer it into the system.DocumentsDirectory as myImage

Would this make sense to you? I have to admit I did not work with file I/O so it is all very confusing still. I found this great info here:

http://www.coronalabs.com/blog/2012/02/14/reading-and-writing-files-in-corona/

Thanks again.

Mo

EDIT: I found exactly what I was looking for here BUT I am not sure if the “bug” people were talking about in that page is still there. I will investigate but the function looks very good and simple enough!

copyFile( “Icon.png”, system.ResourceDirectory, “NewIcon.png”, system.DocumentsDirectory )

[import]uid: 100814 topic_id: 33545 reply_id: 133321[/import]

Try this.

local pathRead = system.pathForFile( "screenShot1.jpg",system.ResourceDirectory )  
local file = io.open( pathRead, "rb" ) --local imageData = file:read( "\*a" )  
io.close( file )  
   
local pathWrite = system.pathForFile( "image.jpg", system.DocumentsDirectory )  
local file = io.open( pathWrite, "wb" ) --file:write( imageData ) --io.close( file )  
file = nil  

[import]uid: 199310 topic_id: 33545 reply_id: 133354[/import]