Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access res/drawable/"folder"

On my application I have many pictures which I have grouped in folder under res/drawable. But Android dosen't let me access them trough "R". Is there a way to access those folders.

This is the Code im using for.

    ImageView iv = new ImageView(conext);
    iv.setImageResource(R.drawable.**smiley.666**);

I marked the part which I can't access.

Thx in Advance

safari

like image 434
safari Avatar asked Sep 26 '11 07:09

safari


3 Answers

No, Android does not allow subfolders under /res/drawable: Can the Android drawable directory contain subdirectories?

You can however, add all image files under /drawable and then access them programmatically via:

int drawableID = context.getResources().getIdentifier("drawableName", "drawable", getPackageName());
iv.setImageResource(drawableID);

Where drawableName is a name of the drawable file, i.e. myimage if image file is myimage.jpg.

The code above will get image resource with id R.drawable.drawableName.

like image 173
Peter Knego Avatar answered Oct 22 '22 02:10

Peter Knego


R.drawable.xxx is just a reference that the Android SDK generates in your R.java file. You are trying to make a sub-folder in your res-folder. The SDK can't generate a reference to any of your sub-folders, you have to put it in the predefined folders drawable-hdpi, -ldpi and -mdpi. If you dont know how this works. I'll sum up. Android runs on a lot of different devices with a lot of different screen resolutions. With these three folders, you can have the same picture in three resolutions and depending on the device you are running your app on, one of these three folders will be used. You can read more here: http://developer.android.com/guide/practices/screens_support.html

like image 38
Finn Larsen Avatar answered Oct 22 '22 04:10

Finn Larsen


You can't create folders in res/drawable the system doesn't recognize those.

like image 2
Thizzer Avatar answered Oct 22 '22 04:10

Thizzer