我正在为 Eclipse 使用 Blackberry JDE Plugin v1.3,我正在尝试使用这段代码来创建一个 BitmapField,我一直都是这样做的:
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);
它工作正常,没有错误,但现在我有这组具有相同名称但在不同子文件夹中的图像,如下所示:
出于解释原因,我把它做得比实际小。我不想重命名这些文件,所以它们都不同。我想知道如何访问不同的子文件夹。 “res/img/on/ico_01.jpg”、“img/on/ico_01.jpg”、“on/ico_01.jpg”是我尝试过但失败的一些例子。
最佳答案
看来EncodedImage.getEncodedImageResource(filename)
将检索 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;
}
关于eclipse - EncodedImage.getEncodedImageResource 无法在 Eclipse 中加载具有相同名称不同子文件夹的图像(黑莓插件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7408273/