How to set image from dynamically dynamically to android?

I save my project-related images in a selectable folder. I also save the image names in a string variable and dynamically try to set these images to an image. But the image is not displayed. Please help me with this.

My code is:

int res = getResources().getIdentifier(imagename, "drawable", this.getPackageName()); imageview= (ImageView)findViewById(R.id.imageView); imageview.setImageResource(res); 

In the above code, "imagename" is a string variable that contains the name of the image.

Thank you in advance

+57
android
Jul 31 '12 at 10:00
source share
14 answers

Try the following:

 String uri = "@drawable/myresource"; // where myresource (without the extension) is the file int imageResource = getResources().getIdentifier(uri, null, getPackageName()); imageview= (ImageView)findViewById(R.id.imageView); Drawable res = getResources().getDrawable(imageResource); imageView.setImageDrawable(res); 
+111
Jul 31 2018-12-12T00:
source share

Here I set the frnd_inactive image from drawable to the image

  imageview= (ImageView)findViewById(R.id.imageView); imageview.setImageDrawable(getResources().getDrawable(R.drawable.frnd_inactive)); 
+34
Jul 31 2018-12-12T00:
source share
 imageview= (ImageView)findViewById(R.id.imageView); imageview.setImageResource(R.drawable.mydrawable); 
+22
Jul 31. 2018-12-12T00:
source share

Try this Dynamic Code

 String fnm = "cat"; // this is image file name String PACKAGE_NAME = getApplicationContext().getPackageName(); int imgId = getResources().getIdentifier(PACKAGE_NAME+":drawable/"+fnm , null, null); System.out.println("IMG ID :: "+imgId); System.out.println("PACKAGE_NAME :: "+PACKAGE_NAME); // Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imgId); your_image_view.setImageBitmap(BitmapFactory.decodeResource(getResources(),imgId)); 

In the above code, you will need the Image-file-Name and Image-View object that you have .

+18
Jul 31 '12 at 10:11
source share

See below code that works for me

 iv.setImageResource(getResources().getIdentifier( "imagename", "drawable", "com.package.application")); 
+8
Jul 31 2018-12-12T00:
source share

This works for me (don't use image extension, just name):

 String imagename = "myImage"; int res = getResources().getIdentifier(imagename, "drawable", this.getPackageName()); imageview= (ImageView)findViewById(R.id.imageView); imageview.setImageResource(res); 
+5
Jun 22 '14 at 2:59
source share

Here is the best way it works on every phone today . Most of these answers are deprecated or need API> = 19 or 22. You can use the fragment code or the action code, which depends on what you need.

Fragment

 //setting the bitmap from the drawable folder Bitmap bitmap= BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.my_image); //set the image to the imageView imageView.setImageBitmap(bitmap); 

activity

 //setting the bitmap from the drawable folder Bitmap bitmap= BitmapFactory.decodeResource(MyActivity.this.getResources(), R.drawable.my_image); //set the image to the imageView imageView.setImageBitmap(bitmap); 

Hurrah!

+5
Dec 14 '16 at 18:03
source share
 imageView.setImageResource(R.drawable.picname); 
+3
May 6 '15 at 10:10
source share

getDrawable() deprecated, so try

 imageView.setImageResource(R.drawable.fileName) 
+3
Nov 18 '16 at 12:00
source share

First, the name of your image is myimage . So what you need to do is go to Drawable and save the image name myimage .

Now suppose that you only know the name of the image, and you need to access it. Use the snippet below to access it,

what you did right, make sure you save the name of the image that you are going to use inside the encoding.

 public static int getResourceId(Context context, String name, String resourceType) { return context.getResources().getIdentifier(toResourceString(name), resourceType, context.getPackageName()); } private static String toResourceString(String name) { return name.replace("(", "") .replace(")", "") .replace(" ", "_") .replace("-", "_") .replace("'", "") .replace("&", "") .toLowerCase(); } 

In addition to this, you must ensure that there are no empty spaces or case sensitivity.

+2
Jul 31 '12 at 10:25
source share

Starting with API 22, getResources().getDrawable() deprecated (see also Android getResources (). GetDrawable () is deprecated API 22 ). Here is a new way to dynamically set an image resource:

 String resourceId = "@drawable/myResourceName"; // where myResourceName is the name of your resource file, minus the file extension int imageResource = getResources().getIdentifier(resourceId, null, getPackageName()); Drawable drawable = ContextCompat.getDrawable(this, imageResource); // For API 21+, gets a drawable styled for theme of passed Context imageview = (ImageView) findViewById(R.id.imageView); imageview.setImageDrawable(drawable); 
+2
Aug 28 '16 at 20:08
source share

This works great for me.

 final R.drawable drawableResources = new R.drawable(); final Class<R.drawable> c = R.drawable.class; final Field[] fields = c.getDeclaredFields(); for (int i=0; i < fields.length;i++) { resourceId[i] = fields[i].getInt(drawableResources); /* till here you get all the images into the int array resourceId.*/ } imageview= (ImageView)findViewById(R.id.imageView); if(your condition) { imageview.setImageResource(resourceId[any]); } 
0
Jul 27 '15 at 10:10
source share

getDrawable () is deprecated. now you can use

 imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.msg_status_client_read)) 
0
Sep 27 '16 at 6:59
source share
 int[] phptr=new int[5]; 

adding image pointer to array

 for(int lv=0;lv<3;lv++) { String id="a"+String.valueOf(lv+1); phptr[lv]= getResources().getIdentifier(id, "drawable", getPackageName()); } for(int loopvariable=0;loopvariable<3;loopvariable++) { et[loopvariable] = findViewById(p[loopvariable]); } for(int lv=0;lv<3;lv++) { et[k].setImageResource(phptr[k]); } 
0
Jun 12 '18 at 6:23
source share



All Articles