问题
I created a static function like this.
public static Bitmap Bitmap(String path) {
Bitmap bitmap = Bitmap
.getBitmapResource(Display.getWidth() + "/" + path);
System.out.println(Display.getWidth() + "" + path);
return bitmap;
}
However, when I called like this,
private Bitmap download = Config_GlobalFunction.Bitmap("btn_download.png");
The output gave me FRIDG could not find 320/btn_download.png
.
In my res
folder, I got an folder which was img
and inside img
got 6 different folders which were 160
, 240
, 320
, 360
, 480
and 640
folder.
How can I call correct folder's image based on Display.getWidth()
?
回答1:
It is possible to have a folder hierarchy under the /res
folder but you must use getClass().getResourceAsStream(path)
rather than Bitmap.getBitmapResource()
in order to create your resource.
This example creates a Bitmap from the path /res/img/hi_res/ui/action_arrow.png
:
String imagePath = "/img/hi_res/ui/action_arrow.png";
InputStream is = getClass().getResourceAsStream(imagePath);
byte[] imageBytes = IOUtilities.streamToBytes(is);
Bitmap b = Bitmap.createBitmapFromBytes(imageBytes, 0, imageBytes.length, 1);
It's a bit more work but it does mean you can have a nice folder structure, rather than hundreds of images lumped together in a single folder.
回答2:
I've had problems with this before. BlackBerry apps don't seem to be well setup to handle resource files in subfolders. That is, you can store your resources in folders, but when bundled into your app (.cod file), they will essentially all be dumped into the same folder.
As you can see, that causes problems if you have multiple resources with the same name (but in different folders).
I used to use the Netbeans IDE to build BlackBerry apps, and with the Netbeans BlackBerry plugin, it seemed to handle this. But, with the RIM JDE, or Eclipse plugin, it doesn't. Perhaps it's something in the ant
build script behind the toolset?
Anyway, I know you would like to do something similar to Android, where you would have:
- res/drawable-hdpi/icon.png
- res/drawable-mdpi/icon.png
- res/drawable-xhdpi/icon.png
and pick the correct version of icon.png based on screen size / resolution. That's a good idea.
However, for simplicity, I would probably recommend changing your system to just use prefixes on your resource names, instead of folders. It's a pain, I know, but BlackBerry seems to handle it better.
So, just call your images:
- res/img/320_btn_download.png
- res/img/360_btn_download.png
- res/img/480_btn_download.png
and then your code can be:
public static Bitmap Bitmap(String path) {
return Bitmap.getBitmapResource(Display.getWidth() + "_" + path);
}
回答3:
if u want to get images depending on their resolutions then...give names to the images according to its resolution like 320x240_img1
, 360x480_img1
. no need to place these images in different folders....dump these images in ur res folder and call like this
int x = Display.getWidth();
int y = Display.getHeight();
String xx = Integer.toString(x);
String yy =Integer.toString(y);
_encImg = EncodedImage.getEncodedImageResource(xx+"x"+yy+".jpg");
来源:https://stackoverflow.com/questions/11538189/blackberry-res-folder-naming-convention