EncodedImage.getEncodedImageResource fail to load image with the same name different subfolder in Eclipse (Blackberry plugin)
I'm using the Blackberry JDE Plugin v1.3 for E开发者_开发技巧clipse and I'm trying this code to create a BitmapField and I've always done it this way:
this.bitmap = EncodedImage.getEncodedImageResource("ico_01.png");
this.bitmap = this.bitmap.scaleImage32(
this.conf.getWidthScale(), this.conf.getHeightScale());
this.imagenLoad = new BitmapField(this.bitmap.getBitmap(), this.style);
It works fine with no errors, but now I have this set of images with the same name but in different subfolders like this:
I made it smaller than it actually is for explanatory reasons. I wouldn't want to rename the files so they're all different. I would like to know how to access the different subfolders. "res/img/on/ico_01.jpg", "img/on/ico_01.jpg", "on/ico_01.jpg" are some examples that I tried and failed.
It appears that EncodedImage.getEncodedImageResource(filename)
will retrieve the first instance of filename
regardless of where it is in your resource directory tree.
This is not very helpful if you have the images with the same filename in different directories (as you have).
The solution I have used is to create my own method which can return an image based on a path and filename.
public static Bitmap getBitmapFromResource(String resourceFilename){
Bitmap imageBitmap = null;
//get the image as a byte stream
InputStream imageStream = getInstance().getClass().getResourceAsStream(resourceFilename);
//load it into memory
byte imageBytes[];
try {
imageBytes = IOUtilities.streamToBytes(imageStream);
//create the bitmap
imageBitmap = Bitmap.createBitmapFromBytes(imageBytes, 0, imageBytes.length, 1);
} catch (IOException e) {
Logger.log("Error loading: "+resourceFilename+". "+e.getMessage());
}
return imageBitmap;
}
精彩评论