RAW
RAW

Reputation: 7825

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

Upvotes: 9

Views: 27708

Answers (5)

user3717188
user3717188

Reputation:

context.getResources().getDrawable(R.drawable.progressbar1);

Android - Open resource from @drawable String

Upvotes: 0

Finn Larsen
Finn Larsen

Reputation: 2199

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

Upvotes: 3

Peter Knego
Peter Knego

Reputation: 80330

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.

Upvotes: 19

htmlove
htmlove

Reputation: 9

you need to save your image first and then make a xml for them so you can access it through R.your_pics

Upvotes: 0

Thizzer
Thizzer

Reputation: 16653

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

Upvotes: 2

Related Questions