Check if file exists

This has been covered a few times it seems, but I have been unable to get it working unfortunately. I found a few threads and come up with this function

--Checks If File Exists  
fileCnt = 0  
function fileExists(fileName)  
 local filePath = system.pathForFile( fileName, '' )  
 local exists = false  
 local fileHandle = io.open( filePath, "r" )  
 if (fileHandle) then  
 exists = true  
 io.close(fileHandle)  
 end  
 fileCnt = fileCnt + 1  
 display.newText( " " .. filePath, 10, fileCnt \* 30, native.systemFont, 10)  
 return(exists)  
end  

Now this works when running in the emulator and displays the full path of my files (so c:\users\stevil\documents\corona\project\assets\img.png) returns true and all runs fine. Put it on my andriod device and it returns false even if the file does exist and the the path isn’t generated (so just shows assets\img.png)

I tried turning it to just return true (as I know it will be true at the moment) and it all worked fine, it loaded the images I was checking fine. The code is something along the lines of:-

fileName = 'assets/img.png'  
if fileExists(fileName) == true then  
 display.newImageRect(fileName, 10,10,100,100)  
end  

That’s just a rough example, it’s used in similar ways in multiple spots.

Am I doing something wrong here? I’ve looked at a few examples and everything appears to be right. I’m wonder if it’s something andriod related I’m missing?

Any help anyone can off me would be greatly appreciated =)
[import]uid: 133056 topic_id: 26339 reply_id: 326339[/import]

Try this function.

[code]
local function doesFileExist(theFile, path)
local thePath = path or system.DocumentsDirectory
local filePath = system.pathForFile(theFile, thePath)
local results = false

local file = io.open(filePath, “r”)

–If the file exists, return true
if file then
io.close(file)
results = true
end

return results
end

–On device example would be
if doesFileExist(“asset.png”, system.ResourceDirectory) == true then
end [import]uid: 84637 topic_id: 26339 reply_id: 106819[/import]

Thanks for replying… sorry for my delay in getting back to you, got caught up with a contract and haven’t had time for my own projects unfortunately =(

What I’m trying at the moment is this:-

local function fileExists(theFile)  
 local filePath = system.pathForFile(theFile, system.ResourceDirectory)  
 local results = false  
 if filePath == nil then  
 return false  
 else  
 local file = io.open(filePath, "r")  
 --If the file exists, return true  
 if file then  
 io.close(file)  
 results = true  
 end  
 return results  
 end  
end  
  
if fileExists("assets/objects/3/worm character.png") == true then  
 --local filePath = system.pathForFile("assets/objects/3/worm character.png", system.ResourceDirectory)  
 local filePath = "assets/objects/3/worm character.png"  
 sheet1 = sprite.newSpriteSheet( filePath, 400, 400 )  
end  

Again, this works on the emulator, but not on the andriod device.

I tried using the system.pathForFile to get the full path for the image, but I get the error


WARNING: Failed to find image(c:\users\stevil\documents\corona projects\test\assets\objects\3\Worm character.png)

I know the images all work, if I load them in maunally (so don’t check for the ones I know that exist) they load fine in both the emulator the device. It’s just if I do the file check to see if they exist, it works locally but not on the device.

Do I have to store them in another directory or something? As you can see above, I’m storing them in just in my own folder in the root of the project called “assets\objects” and then different sub folders to break things up. It’s a bit of a noodle scratcher for me, I know this is probably something that should be easy but just can’t get it to work no matter how much playing around with things I do. [import]uid: 133056 topic_id: 26339 reply_id: 107644[/import]

Okay, since what I was checking for was actually images what I have done is actually loaded the image and if they are still “nil” then I know the file doesn’t exist. This works though I’m sure is not ideal, it also throws a warning each time I do a check and it fails. It does work though, and will do for now =)

Thanks for the help! [import]uid: 133056 topic_id: 26339 reply_id: 107813[/import]