How to access res/drawable/“folder”

2019-01-23 08:31发布

问题:

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

回答1:

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.



回答2:

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



回答3:

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



回答4:

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



回答5:

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

Android - Open resource from @drawable String