Access image file in Resource folder

Dear Forum,

First off, I appreciate all helps so far as I learn more about this fantastic tool! What I’m trying to do is something like this.

Dim f As FolderItem f=GetFolderItem(“0001.jpg") canvas_phantom.Backdrop=f.OpenAsPicture

I have many images that I need to read using different file names that will be structured. I learned from the forum that FolderItem is the best way to do this job.

Then I learned again that I have “app translocation” problem with Mac build because my app works only on some Macs. After reading several instructions, I added “CopyFiles1” (after Build icon) under macOS in the Build Settings and added text.jpg into it. Now I can see through “Show Package Contents” my test.jpg file is copied into Resource folder after building. I managed to find this code to access “test.jpg” in the Resource file.

Dim bundleFile As Xojo.IO.Folderitem bundleFile = Xojo.IO.SpecialFolder.GetResource("test.jpg")

Well, I got this from developer.xojo.com but it gives me an error, “Xojo.Core.InvalidArgumentException”. Anyhow, my question is how can I access test.jpg (which WILL BE copied into Resource folder AFTER building step) during developing and debugging. Do I need to make Resource folder and copy test.jpg while I develop the app? To me, this is confusing.

What does your CopyFiles build step look like? The docs say you get the InvalidArgumentException if the file doesn’t exist. So for some reason, at runtime, the file doesn’t exist in the Resources folder. There could be a number of reasons for this:

  1. The CopyFiles step is not set to occur for both Debug and Release (the default setting is Both, so this is unlikely)
  2. The Destination for the CopyFiles step is not “Resources” (the default setting is “App Parent Folder”)
  3. The file test.jpg doesn’t exist in the source directory before building

If you can’t seem to resolve it when looking at those three possibilities, we’ll need a screenshot of the CopyFiles step in the IDE to help figure out where the files are going.

Also, for what it’s worth, if you prefer to use the Classic Framework FolderItem, you can use my module TPSF to get resources from the CopyFiles steps with the older framework: https://github.com/devtimi/TPSF

Tim,

This is very helpful. I think I was confused with “Applied to” options. One last thing is I was understanding that I have to use the new framework:

  1. To access Resource folder (it seems like not the case by using your TPSF…)
  2. To make my app work on iOS

Does that mean the file names are NOT known at design time? If they ARE known, then I find the easiest way is to copy the .jpg into your application. Then Xojo adds it for you to the Resources folder when built. You can just refer to the .jpg file in code as

canvas_phantom.Backdrop=test

or better yet, in the Paint event of canvas_phantom
g.drawpicture test, 0, 0

Roger

I have about 400 images files named serially: 01.jpg, 02.jpg, 03.jpg, etc. Then I need to use user input to read one of those images. I learned the best way to do this is FolderItem. However, if I can find a way to read images this way after copy all images into my application, I would love to do it and don’t have to worry about Copy Files…

Put all 400 images in a folder, then drag that folder into your app (into the list on the left of the project). It will take a minute or two, but Xojo will add those to your resources folder and you can still access them by your name. Yes, it will increase the size of your executable, but if you’re going to copy them into your Resources folder anyway, then no difference.

Roger, I see what you mean. Good to know there’s no difference in the two methods. Thanks!

The other alternative is not to copy them into your project, but to include them in a folder sitting beside your executable and have your app access them with folder item.openAsPicture

Roger - Do you think it will be another option to resolve app translocation issue on Mac? I used to have my execution file and the image files in the same folder. It works on Windows but only works on some Macs.

I have never dealt with the “translocation” issue, but as I understand it, it happens when the app is run from a removable disk rather than being copied to the hard drive. ( maybe very poor understanding) . Anyhow, I think those issues are about the procedure used and not something peculiar to a particular Mac. The drawback of using the visible folder is that your jpegs are readily available to the user. Granted, unless you encrypt the files, they are still visible in the Resource folder, but it requires a more sophisticated user to find them. If availability is not an issue, then the outside folder is a good option.

Roger,

So I tried both Copy Files and your suggestion (copying files into my app) and I can see test.jpg file under Resources folder. Now I want to access test.jpg from my app as below but the app doesn’t seem to find the file… Am I missing anything?

Dim f As FolderItem f = GetFolderItem("Resources") canvas_phantom.Backdrop=Picture.Open(f.Child("test.jpg"))

When you say “under the Resources folder” I don’t want nderstand what you mean. Drag a file, let’s call it “test.jpg” into the list of item on the left in your project. Now it appears there as “test” ( the .jpg is removed by Xojo)
Now you just write

Canvas_phantom.backdrop = test

[quote=372094:@Jennifer Nober]Roger,

So I tried both Copy Files and your suggestion (copying files into my app) and I can see test.jpg file under Resources folder. Now I want to access test.jpg from my app as below but the app doesn’t seem to find the file… Am I missing anything?

Dim f As FolderItem f = GetFolderItem("Resources") canvas_phantom.Backdrop=Picture.Open(f.Child("test.jpg")) [/quote]
Yeah that won’t work.
Either use Tim’s module to access the resources folder or what Roger suggests, as you’ve added the image to the project, your image is now available in code.

Ive been using this to get the resources folder at run time, if it helps.
A file in that folder can then be referenced using

SpecialfolderResources().child(“A01.jpg”)

But a file called A01.jpg in that folder can be used directly in code like this

dim p as picture
p = A01

[code]//function SpecialfolderResources as folderitem
const myApp =MyAppName"

dim f as folderitem
#if targetMacOS

f =app.ExecutableFile.Parent.Parent.Child("Resources")

#else

#if debugBuild
  f =app.executableFile.parent.parent.child("Debug" + myApp + " Resources")
#else
  f =app.ExecutableFile.Parent.Child(myApp + " Resources")
#endif

#endif

return f
[/code]

In your copy step set applies to both (so it will executed for debug and build), and Destination to Resource Folder (so the getResource method will work)

Basically you need to forget GetFolderItem ever existed.