Picture.Open from GetResource folder

Xojo.IO/SpecialFolder.GetResource returns Xojo.IO.FolderItem
Picture.Open requires FolderItem
Casting between the two types doesn’t work.
So how to read in an image file from the Resources folder?
TIA

Dim pic As Picture
Dim fi As Xojo.IO.Folderitem // Dim fi As Folderitem doesn’t work

fi = Xojo.IO.SpecialFolder.GetResource(filePath.ToText)
If fi <> Nil Then
Return Picture.Open(FolderItem(fi)) // *** this cast fails ***
End if

Return Nil

Which platform ? iOS ? Or did you place a Using Xojo.Core somewhere ?

It usually helps posting in the relevant channel.

Also, when you post code, it would be a great deal more legible if you select it and click the code icon above the editor so it formats it correctly.

OSX

I didn’t place “Using Xojo.Core” in the code. This is the first I’ve heard of it. I’ve just read some help on it but not sure why it is needed.

Instead of using GetResource, better get TPSF from Tim Parnell at
https://forum.xojo.com/15318-modules-for-you-updated-12-17/0

And use the classic framework :

Function getPic(PicName) as Picture Dim f as folderItem = TPSF.Resources.child(picName) if f <> nil and f.exists then Return Picture.Open(f) else Return nil end if end Function

You can actually convert from the new folderitem to an old one using the Path property, thusly:

[code]Dim pic As Picture
Dim fi As Xojo.IO.Folderitem // Dim fi As Folderitem doesn’t work

fi = Xojo.IO.SpecialFolder.GetResource(fileName.ToText )
If fi <> Nil Then
Dim gi as Folderitem = GetFolderItem(fi.path, Folderitem.PathTypeNative)
Return Picture.Open(FolderItem(gi)) // *** this cast fails ***
End if

Return Nil[/code]

GetResource takes a file name, not a path.

Yes I never changed the filepath name back to filename. I was trying to store some items in a subfolder of Resources. It didn’t seem to work.

Greg’s idea seems to work.

But GetResource generates an exception if the file is missing. So I’ve tried to handle it, but for some reason the code stops rather than executing the Catch, displaying the error as xojo.Core.InvalidArgumentException

Dim xfi As Xojo.IO.Folderitem
Dim fi As Folderitem

fileName = fileName + “.png”
Try
xfi = Xojo.IO.SpecialFolder.GetResource(fileName.ToText) // *** stops here
Catch e As xojo.Core.InvalidArgumentException
Return Nil
End Try
fi = GetFolderItem(xfi.path,Folderitem.PathTypeNative)
Return Picture.Open(fi)

Right and it will do that in the debugger. Just click the continue button. It will work properly at runtime.

Thank you both for your help.