views:

417

answers:

4

How do I get bundled images with the BlackBerry Eclipse Plugin 1.1 Beta? I copied an image into "res/background.jpg" and tried to load it using Bitmap.getBitmapResource on background.jpg. Unfortunately, the image wasn't found (Illegal argument exception). I tried moving my image file into the src folder as per the advice here, but that didn't work either. I have opened up the .jar file and the background image is present at the root of the .jar file. The option to convert image files to .png isn't selected either.

Links

A: 

This should be the code to load your image, without any directory in the path.

Bitmap.getBitmapResource("background.jpg")

Then put the image in any package of your src directory like in

com.rim.sample.resources
Michael B.
A: 

When you say it failed what do you mean? Does it throw an error?

I've found that when I add/change a resource it might not take affect immediately. What I found I need to do is to delete the cod file and run a clean on the project (not just build it), then run the packaging (rapc) command. This should make sure the image is there.

Tamar
Well, I know the resource is in the jar, so shouldn't it be in the cod?
Casebash
+3  A: 

Two things:

  1. The res folder needs to be a "source directory" in Eclipse. You can achieve this by right-clicking on the folder and going to "Build Path" -> "Use As Source Folder".
  2. When calling Bitmap.getBitmapResource(), like Michael B. said above, you shouldn't put the folder name in the path. Just call Bitmap.getBitmapResource("background.jpg");

Finally, I'm assuming that because your resource is called "background.jpg" you want it to be the background for a Screen. If this is the case, make sure to set the background using the Screen's Main Manager instead of on the screen itself. For example:

public class MyScreen extends MainScreen
{
   public MyScreen() {
      getMainManager().setBackground(
        BackgroundFactory.createBitmapBackground(
          Bitmap.getBitmapResource("background.jpg")));
   }
}
Skrud
I've made res a source directory (now has that strange brown square divided into quadrants in the upper right hand corner). But it still doesn't work :-(
Casebash
Also, BackgroundFactory was introduced in 4.6. So I can't use that
Casebash
I tried this solution, it didn't work at first, but then it randomly started working. I really have no idea why - I had cleaned and rebuilt several times already.
Casebash
Hmm ... I think sometimes it might not work unless do Project -> Build All Active Blackberry Configurations (as oppose to an Eclipse Workspace re-build), but I'm just speculating here.Glad it worked for you in the end!
Skrud
+1  A: 

When you added the resource to the res folder, did you perform a refresh of the project in Eclipse? If it doesn't 'see' it, the packager ignores it. If you restarted Eclipse, it would pick it up the next time, which might explain why it started working randomly.

Andrew