0

I am absolutly new in Android development and I have the following problem.

I have an image named colosseum_icon.png into the drawable diretory of the res folder and I have to retrieve it into my Java code. So I tryed to use the R class in this way:

Image image = R.drawable.colosseum_icon;

but it give me an error because it seems that in this way return an int value (maybe the identifier of this resource? or what?)

What am I missing? How can I retriev the previous image into my Java code?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • Possible duplicate of [When and why should one use getResources()?](http://stackoverflow.com/questions/7788390/when-and-why-should-one-use-getresources) – Mohammed Aouf Zouag Jun 04 '16 at 18:29
  • "I have an image named colosseum_icon.png into the drawable diretory of the res folder" -- please bear in mind that `res/drawable/` is a legacy synonym for `res/drawable-mdpi/`. It is unlikely that you really want to put this icon in `res/drawable/`. "I have to retrieve it into my Java code" -- what exactly do you intend to do with it? An `Image` class does not really have much of anything to do with a drawable resource. "maybe the identifier of this resource?" -- correct. – CommonsWare Jun 04 '16 at 18:32
  • Tip: If you are loading large images and want to avoid a potential future out of memory error, consider using an image loading library like [Picasso](http://stackoverflow.com/questions/21158425/picasso-load-drawable-resources-from-their-uri) – OneCricketeer Jun 04 '16 at 19:22

4 Answers4

3

you can do this:

Drawable drawable = getResources().getDrawable(R.drawable.colosseum_icon);
Amir_P
  • 8,322
  • 5
  • 43
  • 92
2

First of all you need a Context (i.e. an Activity or Service) to retrive resourse like images embedded in your app.

If your are trying to get an Image (Abstract class) or Bitmap object from an activity class:

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

If your a new to Android dev please take look at this about Context:

https://stackoverflow.com/a/18327529/4562521

Community
  • 1
  • 1
christian mini
  • 1,662
  • 20
  • 39
1

Try this:

1) Find your ImageView field:

ImageView imageView = (ImageView)findViewById(R.id.your_image_field_id);

2) Set your png icon to that field:

imageView.setImageResource(R.drawable.your_icon_name);

I think it will help you.

Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
EugeneM
  • 36
  • 3
0

You have to use a context

You can get it as a bitmap by using.

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                                               R.drawable.colosseum_icon);
Community
  • 1
  • 1
Lionel Briand
  • 1,732
  • 2
  • 13
  • 21