Hi,
How do I verify the existence of a file in the resource folder on android?
thanks
Hi,
How do I verify the existence of a file in the resource folder on android?
thanks
local fileName = "myfilename.etc" -- case sensitive local path = system.pathForFile( fileName, system.ResourceDirectory ) local file = io.open( path, "r" ) -- attempts to open the file for read access if ( file == nil ) then -- file does not exist. else -- file exists. end
thanks ksan
I had problems with png files I found the solution here
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.
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.
Shouldn’t the cat.png be in quotes or is it a table that holds the file name in png field?
Do you really have the image saved as a .txt file?
Not sure about this just ideas to try:
- Remove one period from the file name, make it like catpng.txt
- In the copyFile function try to suffix the images folder to the path instead of prefixing it in file name:
copyFile( “catpng.txt”, system.ResourceDirectory…"/images", cat.png, system.DocumentsDirectory, true )
oh sorry the cat.png is in quotes, you are right. (I did have a table in that place and did replace it with cat.png instead of “cat.png”)
I’ll try the second advice and let you know whatever the result is.
It did not work, I’ve simplyfied things, and now I still doesn’t work.
Here’s my code I don’t get why it isn’t working…
[lua]
function copyFile( srcName, srcPath, dstName, dstPath )
--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 (rfh)
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
--clean up file handles
rfh:close()
wfh:close()
end
copyFile( “backgrass.txt”, nil, “backgrass.png”, system.DocumentsDirectory)
local catImage = display.newImage( “backgrass.png”, system.DocumentsDirectory, 0, 100 )
[/lua]
the ADB log states:
nil --this is the first print I use
/data/data/some app/app_data/backgrass.png – second print statement
Runtime error
bad argument #1 to ‘open’ (string expected, got nil) – this is the same nil as in my first print
stack traceback:
[C] : in function ‘open’
?: in function ‘copyFile’
?: in main chunk
as I can see it looks like the pathforfile module isn’t working as it should.
and the file backgrass.txt is obviously in my resourcefiles…all lowercase.
local fileName = "myfilename.etc" -- case sensitive local path = system.pathForFile( fileName, system.ResourceDirectory ) local file = io.open( path, "r" ) -- attempts to open the file for read access if ( file == nil ) then -- file does not exist. else -- file exists. end
thanks ksan
I had problems with png files I found the solution here
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.
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.
Shouldn’t the cat.png be in quotes or is it a table that holds the file name in png field?
Do you really have the image saved as a .txt file?
Not sure about this just ideas to try:
- Remove one period from the file name, make it like catpng.txt
- In the copyFile function try to suffix the images folder to the path instead of prefixing it in file name:
copyFile( “catpng.txt”, system.ResourceDirectory…"/images", cat.png, system.DocumentsDirectory, true )
oh sorry the cat.png is in quotes, you are right. (I did have a table in that place and did replace it with cat.png instead of “cat.png”)
I’ll try the second advice and let you know whatever the result is.
It did not work, I’ve simplyfied things, and now I still doesn’t work.
Here’s my code I don’t get why it isn’t working…
[lua]
function copyFile( srcName, srcPath, dstName, dstPath )
--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 (rfh)
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
--clean up file handles
rfh:close()
wfh:close()
end
copyFile( “backgrass.txt”, nil, “backgrass.png”, system.DocumentsDirectory)
local catImage = display.newImage( “backgrass.png”, system.DocumentsDirectory, 0, 100 )
[/lua]
the ADB log states:
nil --this is the first print I use
/data/data/some app/app_data/backgrass.png – second print statement
Runtime error
bad argument #1 to ‘open’ (string expected, got nil) – this is the same nil as in my first print
stack traceback:
[C] : in function ‘open’
?: in function ‘copyFile’
?: in main chunk
as I can see it looks like the pathforfile module isn’t working as it should.
and the file backgrass.txt is obviously in my resourcefiles…all lowercase.