Android - How to show images from resources?

I can not show the image that is saved in the res / drawable folder. For this I use ImageGetter. Code below:

ImageGetter imageGetter3 = new ImageGetter() { public Drawable getDrawable(String source) { int id=0; if (source.equals("smiley")) { id = R.drawable.smiley; } Drawable d = getResources().getDrawable(id); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); return d; } }; directions += "Je bent bij lokaal " + vertrek.getNaam() + "\n" + "Sta met je rug voor de deur\n" + Html.fromHtml("<img src=\"smiley\">", imageGetter3, null) + " Draai naar links\n"; 

What I see on the screen at startup is a small square with the text "obj". So what is wrong? Image cannot be read or something else? How to display images?

Honestly, I have a lot of Googled and tried other ImageGetter methods, but none of them seem to work, I also tried them, they do not work:

 ImageGetter imageGetter2 = new ImageGetter() { public Drawable getDrawable(String source) { Drawable d = null; d = Drawable.createFromPath(source); d.setBounds(0,0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); return d; } }; ImageGetter imageGetter = new ImageGetter() { public Drawable getDrawable(String source) { Drawable drawFromPath; int path = Route.this.getResources().getIdentifier(source, "drawable","com.prj56.tracingapp"); drawFromPath = (Drawable) Route.this.getResources().getDrawable(path); drawFromPath.setBounds(0, 0, drawFromPath.getIntrinsicWidth(), drawFromPath.getIntrinsicHeight()); return drawFromPath; } }; 

==================================================== ======= if (....) {ImageView iv1 = new ImageView (this); iv1.setImageResource (R.drawable.smiley);

  directions += "Je bent bij lokaal " + vertrek.getNaam() + "\n" + "Sta met je rug voor de deur\n"; HERE COMES THE IMAGE! BUT HOW TO DO THIS? It within directions textview... directions += " Draai naar links\n"; } 
+10
source share
6 answers

December 12, 2016 Patch :

getResources (). getDrawable () is deprecated in api 22, so now you should use ContextCompat.getDrawable, for example.

 Drawable d = ContextCompat.getDrawable(context, R.drawable.smiley); 

In your activity, you can call this to get the Drawable resource as a Drawable object.

 Drawable d = getResources().getDrawable(R.drawable.smiley); 

If you want to show your drawing ability in ImageView, you can do this:

 ImageView i = new ImageView(this); i.setImageResource(R.drawable.smiley); 

or in your xml file

 <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/smiley"/> 
+33
source

You can get the image with pImg. At the adapter position, get a string that is the name of the image. The image must be present in the folder with the ability to transfer.

 ImageView prodImg=(ImageView)view.findViewById(R.id.img_proj_name); String pImg=prod.get(position); int resID = view.getResources().getIdentifier("@drawable/"+pImg , "drawable", parentView.getContext().getPackageName()); prodImg.setImageResource(resID); 
+6
source

The easiest way to show this is to write this to your ImageView widget in xml:

android:background="@drawable/your_file_name"

... and make sure you put this image file (be it png or jpg, etc.) in your folder. When you write your_file_name , you only need the header, not the file extension.

I prefer this method to programmatically write in your .java file (as shown in other answers above), because your xml will show you the image in preview, it will not programmatically, it will just show the location for ImageView.

+1
source

The easiest way - You can consider the code below

We can use Imageview setImageResource, see below code for it. put the image in a drawable as shown below.

image_1.png, image_2.png, etc.

 int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", getPackageName()); imageview.setImageResource(resId); 
0
source
  Drawable drawable = getResources().getDrawable(R.mydrawableID); 

Now you can use it as an image, for example: -

 ImageView myImage = findViewById(R.id.yourImageView); 

Now run this -

 myImage.setImageResource(drawable); 
0
source

You must use ContextCompat for backward compatibility features on Android.

  Drawable MyImageDrw = ContextCompat.getDrawable(context,R.drawable.myImage); MyImageDrw .setImageDrawable(MyImageDrw); 
0
source

Source: https://habr.com/ru/post/890434/


All Articles