开发者

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

开发者_Go百科

safari


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.


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


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


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


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

Android - Open resource from @drawable String

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜