I think I’ve tried about everything to copy my file from the resource folder to the documents folder. But it isn’t working on the device. Though I am sure the file does exist on this location, because on IOS and in the simulator there is no problem using it.
I know the Resource directory can’t be used for a lot of image files, therefore I like to copy the files to the documentsdirectory.
I used this code
[lua]
function copyFile( srcName, srcPath, dstName, dstPath, overwrite )
print ("started copyFile function for: "…srcName)
local results = false
local srcPath = doesFileExist( srcName, srcPath )
if ( srcPath == false ) then
return nil – nil = source file not found
end
--check to see if destination file already exists
if not ( overwrite ) then
if ( fileLib.doesFileExist( dstName, dstPath ) ) then
print (“file already exists (don’t overwrite)”)
return 1 – 1 = file already exists (don’t overwrite)
end
end
--copy the source file to the destination file
local rfilePath = system.pathForFile( srcName, srcPath )
local wfilePath = system.pathForFile( dstName, dstPath )
print (rfilePath)
print (wfilePath)
local rfh = io.open( rfilePath, “rb” )
local wfh = io.open( wfilePath, “wb” )
print ("file copied: "…srcName)
if not ( wfh ) then
print( “writeFileName open error!” )
return false
else
--read the file from ‘system.ResourceDirectory’ and write to the destination directory
local data = rfh:read( “*a” )
if not ( data ) then
print( “read error!” )
return false
else
if not ( wfh:write( data ) ) then
print( “write error!” )
return false
end
end
end
results = 2 – 2 = file copied successfully!
--clean up file handles
rfh:close()
wfh:close()
return results
end
[/lua]
The “rfilePath” returs nil and this seems to be the problem. But I just can’t figure out what is up with the code.
This is the code I use to call the copyFile function:
[lua]
copyFile( “images/cat.png.txt”, system.ResourceDirectory, “cat.png”, system.DocumentsDirectory, true )
[/lua]
I also tested:
[lua]
copyFile( “images/cat.png.txt”, nil, “cat.png”, system.DocumentsDirectory, true )
[/lua]
can anyone help me with this? Thanks in advance.