Martin
Martin

Reputation: 177

How to accès images from drawable folder?

Im trying to create gallery application. In tutorial I've found, I need to do get images using R.drawable.image_name. I've created folder drawable under /res folder, copied my images one by one into that folder, al of them are .png. But when I try to access them by writing R.drawable.image I can't do that because there is no images I've copied. How to fix it?

Upvotes: 0

Views: 54

Answers (2)

Kavis
Kavis

Reputation: 98

Do clean build or if you are using android studio do invalidate cache and make project afterwards.

For Accessing Drawable:

Drawable drawable = this.getResources().getDrawable(R.drawable.image_name);

If you want bitmap :

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.image_name);

if you want to access by url :

imgUrl= "drawable://" + R.drawable.image_name;

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75645

Clear and rebuild your project. If you got automatic builds disabled, your IDE will not refresh R class contents and won't autocomplete your assets IDs.

Upvotes: 1

Related Questions