SpecialFolder.GetResource InvalidArgumentException

Is there a way to see if a file exists on IOS in Resources?

I’m trying to load an image, but if the image doesn’t exist, I get an InvalidArgumentException before I can see if theFile exists.

              theFile = SpecialFolder.GetResource( theRecordSet.Field("ID").TextValue + ".png" )
              if theFile.Exists then
                theImageFromFile = iOSImage.FromFile( theFile ).ToData( "public.png" )
              end if

I also tried wrapping it in try/catch, but that didn’t help.

            try
             catch e as InvalidArgumentException
            end try

Also, should the helpdocsonline example for SpecialFolder.GetResource read as:
bundleFile = SpecialFolder[b].GetResource/b

[quote=154706:@Hal Gumbert]Also, should the helpdocsonline example for SpecialFolder.GetResource read as:
bundleFile = SpecialFolder.GetResource(“AppDatabase.sqlite”)

[/quote]

Yes.

There seems to be an error in the LR.

Is there a way to test if a file in resources exists? Right now I get an InvalidArgumentException. :frowning:

You should put the file name you are using into system.debuglog and see what is actually in there. Looks as if what you are passing to getresource is an invalid file name.

Exactly. I know for sure the file doesn’t exist. What I want to do is load a file IF it’s in the resources. So, if it’s there, I want to use it. The problem is that I’m getting an error that I can’t seem to suppress.

I think this is a bug. In Desktop, such a code would return a nil folderitem. Here it crashes.


OS: OS X 10.10.1

Xojo: 2014R3.1

Steps: When the file SpecialFolder.GetResource is looking for does not exist, the app crashes.

See attached project.

The code in action works fine with “blue.jpeg” (exists), but crashes when “dblue.jpeg” (does not exist).

<https://xojo.com/issue/37504>

As a workaround, you may want to list the files in the bundle, and compare with the name of your file to verify it is there. Here is what I use to fill a TableView with the files in the bundle :

[code] dim f as folderitem = SpecialFolder.GetResource(“Info.plist”)
f = f.Parent

Table1.AddSection(“”)
Dim cell As iOSTableCellData

For Each myFile As FolderItem In f.Children
//whatever you want to do with each item (myFile) here
system.DebugLog myFile.name
cell = New iOSTableCellData(myFile.Name)
Table1.AddRow(0, cell)
Next[/code]

Are you not able to wrap your code in a try-catch and do something else if the InvalidArgumentException is raised?

GetResource appears to carry an assumption that the file will always exist, that it’s something that would have been bundled with the original app, and is probably part of the signature. Anything that might or might not exist would probably be in ApplicationSupport, Caches, Documents or Temporary.

[quote=154987:@Greg O’Lone]Are you not able to wrap your code in a try-catch and do something else if the InvalidArgumentException is raised?

GetResource appears to carry an assumption that the file will always exist, that it’s something that would have been bundled with the original app, and is probably part of the signature. Anything that might or might not exist would probably be in ApplicationSupport, Caches, Documents or Temporary.[/quote]

Apparently, it is not an InvalidArgumentException when the file is missing.

I placed a system.debuglog exc.Reason in App.Unhandled Exception and I get :

toto.png not found

What exception could that be ?

EDIT : I see that the bug report I filed about that has been closed as “by design”, since the error should be handled.

Fair enough. Now it took me a bit of experimentation to find that it is simply a RuntimeException. It would be nice to change the LR.

37517 - iOS GetResource triggers Runtime exception if file missing : please tell in LR

Steps: At SpecialFolder SpecialFolder.Getresource shows how to get the path to a file in the bundle.
It works quite well if the file is present, but if it is missing, it generates a RuntimeException.

It would be nice to mention it, so users can use a Try Catch to avoid a crash of the app.

<https://xojo.com/issue/37517>

I also tried catching the InvalidArgumentException, but it still didn’t work as Michel found.

Greg, I see what you are saying about it assuming that the file exists, but all the other SpecialFolder locations don’t do that. Seems odd that the Resources folder behaves differently. I hope you had a great Christmas!

Are you saying code like

theFile = SpecialFolder.GetResource( theRecordSet.Field("ID").TextValue + ".png" )
   if theFile.Exists then
      theImageFromFile = iOSImage.FromFile( theFile ).ToData( "public.png" )
   end if
catch e as InvalidArgumentException
       // no such file
end try

doesn’t work for you ?
GetResource assumes the file exists because … well … its supposed to be a “resource” IN the app bundle
And if your app is missing resources then its probably not quite the way you intended
Nothing else assumes this because they’re not looking inside the app bundle

[quote=156288:@Hal Gumbert]I also tried catching the InvalidArgumentException, but it still didn’t work as Michel found.

Greg, I see what you are saying about it assuming that the file exists, but all the other SpecialFolder locations don’t do that. Seems odd that the Resources folder behaves differently. I hope you had a great Christmas![/quote]

You apparently did not fully read what I posted :

Catch a RuntimeException, and you will be fine.

[quote=156288:@Hal Gumbert]I also tried catching the InvalidArgumentException, but it still didn’t work as Michel found.

Greg, I see what you are saying about it assuming that the file exists, but all the other SpecialFolder locations don’t do that. Seems odd that the Resources folder behaves differently. I hope you had a great Christmas![/quote]
I believe that the resources folder is read only though.

Happy New Year!

Thanks Michel… I’ll try checking for the RuntimeException.

Norman, yes that code fails when the file doesn’t exist. I expected GetResource to work like Temporary or Document. It’s weird that getting a file from the resources acts differently.

My purpose was to load images into the database during development. I’d copy images to the Resources when I want to change an iOSTable image IF the image exists in Resources. For now, I comment out the code when not adding images and then run the project again.

[quote=156353:@Hal Gumbert]Happy New Year!

Thanks Michel… I’ll try checking for the RuntimeException.

Norman, yes that code fails when the file doesn’t exist. I expected GetResource to work like Temporary or Document. It’s weird that getting a file from the resources acts differently.

My purpose was to load images into the database during development. I’d copy images to the Resources when I want to change an iOSTable image IF the image exists in Resources. For now, I comment out the code when not adding images and then run the project again.[/quote]
Define “fails” ?
In the debugger you will get the exception shown but press resume and things should carry on merrily

That’s correct Norman. It just causes the exception. Still seems strange that it works differently than the other special folders.

Not at all
“Resources” are things that should be part of the app bundle when you create it, sign it, etc
So if they are missing you’ve got other problems

Not in all cases. There are cases where you’d want to see IF a resource exists. In my case, I’m using it to update an image to a database IF it exists. So it’s not a problem.

Being able to use at RuntimeException fixes my problem, but it does behave differently than the other SpecialFolders which is why it seems odd. Since all the other SpecialFolders return nil. I expected GetResource to do the same.

I wasn’t allowing users to change it. It was for me. I suppose I could leave the images as resources permanently. I just planned to take them out when they are already in my database.

My code is now rocking with the RuntimeException. And I get your reasoning. It’s just strange that that one SpecialFolder works differently than the others.

Heh. You should see SpecialFolder on Xojo Cloud then. :slight_smile:

Dont remove them
They ARE part of the contents of your app and are signed once you do that

In fact I’d guess the OS would properly prevent you from doing that much like trying to write to a bundle on OS X is prevented