• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Folders in Drawable

jmgmail

Lurker
Hello. If I want to get an image from drawable folder I do this:

getResources().getDrawable(R.drawable.myimage)

But if I want to get an image in a folder like this:

drawable/pngs/myimage.png

How I can get that image?
 
That din'nt work. I don't know if I'm doing something wrong or these things don't work in Android.

For example, in all these codes, never retrieves the text of the resorce. Allways say "source not found"
s = getClass().getResource(​
"iconos/regalo.png").toString();

s = getClass().getResource(​
"/iconos/regalo.png").toString();

s = getClass().getResource(
"ic_launcher.png").toString();
 
Maybe it is not posible, because I have 5 images and the system only detects 4:

yw06.png

zs46.png
 
This Works:
int resID = getResources().getIdentifier("com.mydomain.myapp:drawable/ic_launcher", null, null);

This does not work (the value alwais is 0):

resID = getResources().getIdentifier(

"com.mydomain.myapp:drawable/iconos/regalo", null, null);

resID = getResources().getIdentifier(​
"com.mydomain.myapp:drawable.iconos/regalo", null, null);

resID = getResources().getIdentifier(​
"com.mydomain.myapp:drawable/iconos.regalo", null, null);

resID = getResources().getIdentifier(
"com.mydomain.myapp:drawable.iconos.regalo", null, null);


If I can get the "ID" I could use the image, but I think there is no way to obtain a resource in a folder because the system can't create the ID in the R.java file:
A part of R.java file:
public​
staticfinalclass drawable {

publicstaticfinalintic_launcher=0x7f020000;

publicstaticfinalintmapa=0x7f020001;

publicstaticfinalintsobre=0x7f020002;

publicstaticfinalinttarjetacredito=0x7f020003;

}

Can someone help me to make it works?
 
Hello. I ran into the exact same problem with the app I'm working on. Unfortunately, as far as I can tell from about 2 hours of research, there is no real way to use subfolders in drawable folders. There are two "solutions" however. You can either use underscores in the name, like "drawable_pngs_myimage", or you can put your images in the assets folder. Neither are exactly desirable, but I wound up using the assets folder method (as my game is in OpenGL, so I don't really need different screen sizes anyway...). You should be able to load your images from the assets folder like this:

Code:
InputStream is = null
try{
is = context.getAssets().open(filepath);
}catch(Exception e){
//TODO: Insert "placeholder" image for non-valid images
}

Bitmap bitmap = BitmapFactory.decodeStream(is);

The code is pretty much ripped right out of my game, with added local variable and comments to make it a bit more readable. Hope this helps!
 
I also ran into the same issue recently and I was actually just about to say the same thing, only someone beat me too it! :D thanks for sharing though.
 
Back
Top Bottom