8

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

safari
  • 7,565
  • 18
  • 56
  • 82
  • 1
    I think there must be **import com.android.R;** or some else R imported in your file just delete it and build your project again it may be referring to wrong R.java file so reference is generated....... – Hanry Sep 26 '11 at 07:34
  • possible duplicate of [Can the Android drawable directory contain subdirectories?](http://stackoverflow.com/questions/1077357/can-the-android-drawable-directory-contain-subdirectories) – user2864740 May 14 '14 at 08:44

5 Answers5

19

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.

Community
  • 1
  • 1
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • If my picture is called "angel.gif" i have to change drawableName into "angel" right? Thx for your help! – safari Sep 26 '11 at 08:51
  • Just check this question also: http://stackoverflow.com/questions/4930398/can-the-android-layout-folder-contain-subfolders – Paresh Mayani Sep 26 '11 at 12:35
3

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

Finn Larsen
  • 2,201
  • 17
  • 26
2

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

Thizzer
  • 16,153
  • 28
  • 98
  • 139
  • +1, yes exactly i am totally agree with you.@Safari, you just check this link: http://stackoverflow.com/questions/4930398/can-the-android-layout-folder-contain-subfolders – Paresh Mayani Sep 26 '11 at 07:58
0
context.getResources().getDrawable(R.drawable.progressbar1);

Android - Open resource from @drawable String

Community
  • 1
  • 1
0

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

htmlove
  • 9
  • 1