Home > Mobile >  How do I access an image as a Drawable in AndroidStudio?
How do I access an image as a Drawable in AndroidStudio?

Time:06-10

I'm working on an android app using java in AndroidStudio. I have an ImageView pic and I want to set its image as a .png that I have saved in my /drawable folder. I may try

pic.setImageDrawable(R.drawable.image_name);

However, setImageDrawable() requires a Drawable, while R.drawable.image_name returns an Integer (the ID of the image).


How may I resolve this?

CodePudding user response:

You can use setImageResource() for this:

pic.setImageResource(R.drawable.image);

More about it here

CodePudding user response:

Use this for java

ContextCompat.getDrawable(getActivity(), R.drawable.drawable_resource_name);

and then set this drawable to your image view

  • Related