Drawable resource using a variable - android

I want to use a variable instead of R.drawable.myimage because I have a database with the image's names. I get the name from the database and I want to use the drawable resource with this name.
String flagimage = "R.drawable." + myHelper.getFlagImage(5);
int resourceId = Integer.parseInt(flagimage);
Bitmap flag = BitmapFactory.decodeResource(getResources(), resourceId);

You can use the name of a resource like
getIdentifier (String name, String defType, String defPackage);
getResources().getIdentifier("us","drawable","com.app");
The above function will return an integer value same as R.drawable.us.
This is how you access with resource names.

You can use this code to get the identifier...
Resources res = this.getResources();
int resID = res.getIdentifier(imagename, "drawable", this.getPackageName());

Related

Use the address in the database for imageview

I'm sorry for my English .
I have a SQLite database of stored name of images in the drawable folder.
Example : R.drawable.image1 , R.drawable.image2 , ...
Is there a way that addresses stored in the database attributed to ImageViews?
Yes, you can get a Drawable by it's name, but it needs to be the full name of the resource, including the extension. So, for instance, if "R.drawable.image1" refers to an image named "image1.png", you will need to look it up by the string "image1.png".
Given the name of the image (e.g. "image1"), you can use the following method to get the resource id of the image (assuming that all of your images have the ".png" extension):
private final Map<String, Integer> iconMap = new HashMap<>();
public int getIconResourceId(final String imageName) {
String name = imageName.replace(".png", "");
Integer resourceId = iconMap.get(name);
if(resourceId == null) {
Resources resources = getResources();
resourceId = resources.getIdentifier(name, "drawable", getPackageName());
iconMap.put(name, resourceId); // cache the entry
}
return resourceId.intValue();
}
Note that the above code caches the resource ids - I use this in code for a RecyclerView, where the same image may need to be retrieved often, while scrolling the RecyclerView. If you don't need that, you may want to remove the HashMap.

How to link database record with drawable id?

In my android application I have categories that stored in categories table of database. And I want to assign icons for each category. Category table looks like:
The problem is that I am not able to use drawable constants that defined in R file because they are not static and will be changed from build to build.
Is it correct to create public.xml file and define all drawable constants inside the file?
I am afraid that I can override some android constants using this approach.
Why don't you simply use the drawable names?
You can get the resource id at run time, for the given resource name and type.
Add this method to your code:
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
And use it like this:
int myID =
getResourceID("your_resource_name", "drawable", getApplicationContext());
Note: no path nor extension, in case of images.
I think you need this:
int imageResource = context.getResources().getIdentifier(imageName,
"drawable", context.getPackageName());
and if you want to set the image to an imageView:
// set the image
if (imageResource != 0)
yourImageView.setImageResource(imageResource);

How to get Resource name?

How to get image name and display it in a textbox?
I used arraylist that generates random images.
String ImageName = getResources().getResourceName((Integer)list.get(position));
The output I get from this code is R.drawable.image1. How to get the image name image1?
try this,
String name = getResources().getResourceEntryName(image[position]);
Try this
string.substring(string.lastIndexOf("."),string.length());
String name = "image1";
int imgId = getResources().getIdentifier("drawable/" + name, "drawable", getPackageName());

Access a image dynamically from res folder android

I have a image in res folder like this
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 ?
Resources res = getResources();
String mDrawableName = "image_name";
int resourceId = res.getIdentifier(mDrawableName , "drawable", getPackageName());
Drawable drawable = res.getDrawable(resourceId);
icon.setImageDrawable(drawable );
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));
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.

get Image Name in ImageView Android

I am dynamically loading images in image view by setting
view.setBackgroundResource(cards[i])
// cards is the image array
cards[] = {R.drawable.image1,R.drawable.image2....}
is there any way I can know the image name or image id associated with image view in android?
You can use the name of a resource like
getIdentifier (String name, String defType, String defPackage);
int id = getResources().getIdentifier("us","drawable","com.app");
The above function will return an integer value same as R.drawable.us.
This is how you access with resource names.
To access the resources name
String resName = getResources().getResourceName(R.drawable.imagename)

Categories

Resources