Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a image dynamically from res folder android

I have a image in res folder like this

enter image description here

I want to access this one dynamically like this

holder.Viewcover.setImageDrawable(Drawable.createFromPath("R.id." + CoverimgUrl.get(position)));

CoverimgUrl is a list which have two image name one is book_cover & and another is blank_image this array is generated dynamically so how can I set this image from that list

In One word how to access a image dynamically which is in drawable folder and I need get that image name from an array list ?

like image 361
Anirban Avatar asked Apr 18 '13 06:04

Anirban


People also ask

How do I view an image in a specific folder Android?

You can create a File object with the path of your location, then create a Uri from that File. For example: File myFile = new File("/scard/myfolder"); Uri myUri = Uri. fromFile(myFile);

How do I find my pictures from the drawable folder on Android?

Then you can see the drawable folder under app —> res folder in the panel below the subview. Copy images from any directory you saved and right-click the drawable folder, click the Paste menu item in the popup menu list then accept all popup dialog. After that, you can see the images added in the drawable folder.

How can add image in res folder in Android?

Step 1: Open Android Studio and go to the app > res > right-click > New > Image Asset as shown in the below figure. Step 2: A pop-up screen will arise like below. Here choose Action Bar and Tab Icons in Icon Type. Step 3: Then choose Asset Type as Image and enter the Path of your image.

What is resource folder in Android?

The res/values folder is used to store the values for the resources that are used in many Android projects to include features of color, styles, dimensions etc.


Video Answer


3 Answers

Resources res = getResources();
String mDrawableName = "image_name";
int resourceId = res.getIdentifier(mDrawableName , "drawable", getPackageName());
Drawable drawable = res.getDrawable(resourceId);
icon.setImageDrawable(drawable );
like image 102
Nirav Ranpara Avatar answered Oct 04 '22 12:10

Nirav Ranpara


First Make CoverimgUrl list of integer

List<Integer> CoverimgUrl =new ArrayList<Integer>();
            CoverimgUrl.add(R.drawable.book_cover);
            CoverimgUrl.add(R.drawable.blank_image);

Then

holder.Viewcover.setImageResource(CoverimgUrl.get(position));
like image 43
Arun C Avatar answered Oct 04 '22 12:10

Arun C


createFromPath expects a path to the file, not it's ID.

You can use the following:

int id = getResources().getIdentifier(CoverimgUrl.get(position), "id", getPackageName()); holder.Viewcover.setImageDrawable(getResources().getDrawable(id));

getIdentifier() gets the ID from the string. When you use the "R" class, it contains static integers for ids. So R.id.some_name is actually an integer, which is the id of the some_name resource.

once you get this integer with getIdentifier, you can use getResources().getDrawable() to get the drawable with the given ID.

Let me know if this works.

like image 25
tbkn23 Avatar answered Oct 02 '22 12:10

tbkn23