Problems determining if image file exists on Android

I am using the code in question 3 here (http://coronalabs.com/blog/2013/02/13/faq-wednesday-sub-folder-and-file-access/) to verify that some files exist before doing some.

My project is set up with all the LUA and font files in the root (Resources) folder.  Then I have an Assets folder in that, which has some files and two other folders - Audio and Images.

So I want to check if “Assets/Images/filename.png” exists.  According to that blog post above, I should be able to do that since the file is in the Resources folder but in a subfolder. 

-- Checking for file in Documents directory local results = doesFileExist( "Images/cat.png", system.DocumentsDirectory ) -- or checking in Resource directory local results = doesFileExist( "Images/cat.png" )

However, when I do a doesFileExist call on “Assets/Images/filename.png”, it always comes back false.

I have the print statements in the doesFileExist enabled so I can see that what paths it is looking for.  The first file I am checking for is “Assets/filename.xml” and that it finds fine.  It checks and finds it at:

/data/data/[package name]/files/coronaResources/Assets/filename.xml

But when it goes to check for the PNG file, the path it is checking is:

Assets/Images/filename.png

Seeing the note about the Android file restrictions, I added a filename.png.txt file and checked for that, and it does correctly check and find it here:

/data/data/[package name]/files/coronaResources/Assets/Images/filename.png.txt

But according to the example, it should be able to find the PNG file in a subfolder, so why isn’t this working?  Is the example wrong?  Can you not check for an image file at all that is located in the Resources folder or one of its subfolders?

I am using build 1202 if it matters.

Hi @thegdog,

I’m pretty sure you can’t check (on Android) for an image in the Resources folder -or- any of its subfolders… the subfolder still being part of the Resources directory. Perhaps I’m not understanding what you mean though?

Brent

No, that is what I am trying to do.

But if that is the case, why does your example show how to check for an image file in the Resources folder?

The system.pathForFile() function will check for a file’s existence for the ResourceDirectory and all other directories.  If that function returns “nil”, then the file does not exist.  If it returns a string, then the file was found.

The example says that:

The file extensions that cannot be read in the Resource directory by Android are: html, htm, 3gp, m4v, mp4, png, jpg, and rtf.

So, you will not be able to check if the files exist or not using that function. What I do to check image is:

local result = false local img = display.newImage("assets/images/image.jpg") if img ~= nil then display.remove(img) result = true end 

Yes, Renato.  But it says in the Resource directory, but does not specify subfolders of that.  The example further goes on to show:

-- Checking for file in Documents directory local results = doesFileExist( "Images/cat.png", system.DocumentsDirectory ) -- or checking in Resource directory local results = doesFileExist( "Images/cat.png" )

Which gave me the impression that you could check images in subfolders since that’s what their example shows happening.

Just one of those things where they need to be mindful of documentation.   :slight_smile:

In the end, I decided I was trying too hard to write clean code and catch any errors from occurring.  I should be able to ensure that the files are in the Resource directory before it ships, so I shouldn’t need to check for them before trying to displaying.

But thanks for the information, and a possible way to check if I ever do need to verify an image is there.

Yes, you can use the

-- or checking in Resource directory local results = doesFileExist( "Images/cat.png" )

but only on iOS devices, not Android.

But you are right, you shouldn’t have to bother testing the resources files via code since they are shipped in your build.

Hi @thegdog,

I’m pretty sure you can’t check (on Android) for an image in the Resources folder -or- any of its subfolders… the subfolder still being part of the Resources directory. Perhaps I’m not understanding what you mean though?

Brent

No, that is what I am trying to do.

But if that is the case, why does your example show how to check for an image file in the Resources folder?

The system.pathForFile() function will check for a file’s existence for the ResourceDirectory and all other directories.  If that function returns “nil”, then the file does not exist.  If it returns a string, then the file was found.

The example says that:

The file extensions that cannot be read in the Resource directory by Android are: html, htm, 3gp, m4v, mp4, png, jpg, and rtf.

So, you will not be able to check if the files exist or not using that function. What I do to check image is:

local result = false local img = display.newImage("assets/images/image.jpg") if img ~= nil then display.remove(img) result = true end 

Yes, Renato.  But it says in the Resource directory, but does not specify subfolders of that.  The example further goes on to show:

-- Checking for file in Documents directory local results = doesFileExist( "Images/cat.png", system.DocumentsDirectory ) -- or checking in Resource directory local results = doesFileExist( "Images/cat.png" )

Which gave me the impression that you could check images in subfolders since that’s what their example shows happening.

Just one of those things where they need to be mindful of documentation.   :slight_smile:

In the end, I decided I was trying too hard to write clean code and catch any errors from occurring.  I should be able to ensure that the files are in the Resource directory before it ships, so I shouldn’t need to check for them before trying to displaying.

But thanks for the information, and a possible way to check if I ever do need to verify an image is there.

Yes, you can use the

-- or checking in Resource directory local results = doesFileExist( "Images/cat.png" )

but only on iOS devices, not Android.

But you are right, you shouldn’t have to bother testing the resources files via code since they are shipped in your build.

It can be as simple as wanting to support either png or jpg for images that the client provides. Exists can’t be used to check for both. Renato’s example does work. Side note that if you use refPointConversions.lua to not have to change all reference points for graphics v2 in a legacy app, you will have to do it if you use Renato’s example, or that script will crash instead. 

It can be as simple as wanting to support either png or jpg for images that the client provides. Exists can’t be used to check for both. Renato’s example does work. Side note that if you use refPointConversions.lua to not have to change all reference points for graphics v2 in a legacy app, you will have to do it if you use Renato’s example, or that script will crash instead.