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.

copyFile
local function copyFile(dstName)
    local results = false
 
    local rfilePath = system.pathForFile( "nophoto.jpg" )
    local wfilePath = system.pathForFile( dstName, system.DocumentsDirectory )
 
    local rfh = io.open( rfilePath, "rb" )
    local wfh = io.open( wfilePath, "wb" )
 
    if  not wfh then
        print( "writeFileName open error!" )
        return false            -- error
    else
        -- 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
        else
            if not wfh:write( data ) then
                print( "write error!" )
                return false    -- error
            end
        end
    end
 
    results = 2     -- file copied
 
    -- Clean up our file handles
    rfh:close()
    wfh:close()
 
    return results
end

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

Any thoughts?

https://docs.coronalabs.com/api/library/system/ResourceDirectory.html#android

https://roaminggamer.github.io/RGDocs/pages/SSK2/

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/files/

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/com.solar2d.app.Donzda/" 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