Skip to content Skip to sidebar Skip to footer

How To Load Images From Resources Folder Into Android Listview

I have a 200 images in resource folder i named them as name_1.png,name_2.png. and I have a array in that I have few numbers such as 2, 4 , 6 ,30 , 40 ... and so on. I want to load

Solution 1:

You can not easily do that when your images are in the res folder since each resource is accessed by an int id.

You should consider to place your images into the assets folder which you can access like a regular file system. Have a look at the AssetManager.

Solution 2:

Try this if you want to fetch images from assets:

InputStream is = ctx.getAssets().open("normal_"+mStrings.get(position)+".png");
Drawable d = Drawable.createFromStream(is, "resourceName");
i1.setBackgroundDrawable(d); //i1 is your imageView

hope it helps !!

Solution 3:

It is simple just do as given below in your getView() method

 String yourimagename="normal_"+ mStrings.get(position);   // In this no need of image extension like .png,.jpg etcint  resImgId= YourActivityName.this.getResources().getIdentifier("Your Package Name:drawable/" +yourimagename, null, null);  

or try this

int  resImgId= YourActivityName.this.getResources().getIdentifier("Your Package Name:drawable/" +yourimagename, null, null); 

or try this

int  resImgId= context.getResources().getIdentifier("Your Package Name:drawable/" +yourimagename, null, null); 
 i1.setImageResource(resImgId);

Solution 4:

If you want to use an image from drawable folder based on its name (getting resource id from resource name) use public int getIdentifier (String name, String defType, String defPackage) which returns a resource identifier for the given resource name like:

int image = getResources().getIdentifier(image_name, "drawable",getPackageName());
 ImageView i1= (ImageView)convertView.findViewById(R.id.ivlfirst);
 i1.setBackgroundResource(image);

For your particular case you can use:

i1.setBackgroundResource(getResources().getIdentifier("normal_"+mStrings.get(position), "drawable",getPackageName()));

Solution 5:

Take One Integer array for R.drawable.imageView1......200 and pass its value Like

privateInteger[] Imgid = {R.drawable.imageview1,....,200  };
    i1.setBackgroundResource(Imgid[j]);

Post a Comment for "How To Load Images From Resources Folder Into Android Listview"