copyFile problem

Hello. I have problem with this copyFile function.
I want to copy file “nophoto.jpg” from resources to documents as “copy.jpg”.
On Corona Simulator on PC it works but on my phone it doesn’t work fine. It copies black screen with 0B, blank jpg.

local function copyFile(dstName)
    local results = false
    local rfilePath = system.pathForFile( "nophoto.jpg" )
    local wfilePath = system.pathForFile( dstName, system.DocumentsDirectory )
    local rfh = rfilePath, "rb" )
    local wfh = wfilePath, "wb" )
    if  not wfh then
        print( "writeFileName open error!" )
        return false            -- error
        -- Read the file from the Resource directory and write it to the destination directory
        local data = rfh:read( "*a" )
        if not data then
            print( "read error!" )
            return false    -- error
            if not wfh:write( data ) then
                print( "write error!" )
                return false    -- error
    results = 2     -- file copied
    -- Clean up our file handles
    return results

I use it this way: copyFile("copy.jpg")

Any thoughts?

With ssk, you’d do that like this:

local src = ssk.files.resource.getPath("copy.jpg") -- File from root folder of project
local dst = ssk.files.documents.getRoot() -- Path to documents folder on current system
ssk.files.cpFile( src, dst )

If this is Android, I wrote this plugin to address this very thing. The Read function is basically an open(filename) + read("*a") call.

1 Like

i tried to copy a image to my : …"/Android/data/" and dosent work, how to manage files in my folder? @roaminggamer

how to get status, onCompletee. I want to show some message ou spin or progressbar while coping my files