Android Question Images and icons in drawable-*dpi folders, how to use in code

PacoViv

Member
Licensed User
Hello, I have several icons with same name (ie. someimage.png) and different sizes (48x48), (72x72) etc. in each drawable-*dpi folder.

I'm not meaning the icon app but any other icon or image. For example an icon or a picture of a cat at the center of the layout.

My question is how can I use those icons in the code?

From the b4a HelpViewer:
Example:
ImageView1.Bitmap = LoadBitmap(File.DirAssets, "someimage.jpg")

But those images are not in dirassets nor direxternal, or should I add those image files that have the same name?, how? ... the concrete image should be get by Android at runtime from the appropiate drawable folder for the device used.

So how must be my code?
Or have I to add any line in the manifest like in the case of the launcher icon?

Thanks in advance
 

MarcoRome

Expert
Licensed User
Longtime User
You take your pictures ( example cat.jpg ) in your folder File ( = File.DirAssets ).
So if you put there a good picture, example 1024 x 800 also if you have device with different resolution you'll have no problems.
Unlike if you post an image of poor quality and small (example 300 x 200), the same could be grainy.
 
Upvote 0

PacoViv

Member
Licensed User
You can access the drawables in drawable-*dpi folder using AndroidResources library. Use the method GetApplicationDrawable and pass the resource name (without extension) as argument.

I tried the demo and it works, but when I want to implement it im my app it does not.
He has 3 modules and I would like to have it in a simple app with only the main module and only one layout.
I see also that he doesn't use the usual drawable-*dpi folders but only one drawable-nodpi folder. !?
I changed the panel that I had with the image for an imageview (in case this could affect) and did a lot of tests changing things.

With the following code in activity create first time (and many others changing things) I get always the same error at run time:

java.lang.RuntimeException: Object should first be initialized (BitmapDrawable).

I tried also initializing it before with another png in the dirassets folder but I get the same error.
The code:
B4X:
Dim BitmapDrawable1 As BitmapDrawable
BitmapDrawable1=AndroidResources1.GetApplicationDrawable("ic_easter_696844_960_720")
ImageView1.Bitmap=BitmapDrawable1.Bitmap

Where ic_easter_696844_960_720 is the name of the png images in the drawable-*dpi folders

Of course I have
Dim AndroidResources1 As AndroidResources in sub process_globals and
Private ImageView1 As ImageView in sub globals
 
Last edited:
Upvote 0
Top