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
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
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)