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”)
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.
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.
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!
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]
[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.
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.
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
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.