get Image Name in ImageView Android - 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)

Related

How to change an ImageView with a filename

I have a string with the name of the file that I want (R.drawable.square) to put in the imageview.
String shape = "square"
I am trying to use the method below to show the image
imageView.setImageResource()
Hardcoding the filename in works but I want to be able to pass different filenames in.
image.setImageResource(R.drawable.square)
tl;dr
can't get from String:"square" to int:R.drawable.square
Please see Android, reference things in R.drawable. using variables?
//to retrieve image in res/drawable and set image in ImageView
String imageName = "picture"
int resID = getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
image.setImageResource(resID );
I achieved the effect by putting the images in the assets directory
I think it's better to use you Strings as key and your resource ids as value in a Map Object.
Map<String, Integer> resources = new HashMap<>();
resources.put("square", R.drawable.square);
// resources.put("...", R.drawable....);
imageView.setImageResource(resources.get("square"));

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 get name of a drawable assets dynamically in android?

I have some photos in my drawable folder res that name of them is like
move1.jpg
move2.jpg
move3.jpg
and so on.
I have a table in my database which has a field to store photo ID.
I have a selected list of items (in my case movement), that I want, user see the proper photo based on the Id of photo.
movements = db.getAllMovements(true);
...
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Dialog movement_image_dialog = new Dialog(getActivity());
movement_image_dialog.setContentView(R.layout.image_dialog);
ImageView movement_imageView = (ImageView) movement_image_dialog.findViewById(R.id.movement_imageview);
**int photoId = finalMovements.get(position).getPhotoId();**
**movement_imageView.setImageResource(R.drawable.*move4*);**
...
}
My question is how I can set the Image resource with the photoId that I have?
I need something like this
R.drawalbe.(move+photoId)
You can use resource getIdentifier(String name, String defType, String defPackage) as
int drawableResourceId = this.getResources().getIdentifier("nameOfDrawable", "drawable", this.getPackageName());
Where
this is an Activity.
nameOfDrawable is move1, move2 etc in your case.
drawableResourceId is the one you can pass in
movement_imageView.setImageResource(drawableResourceId);
we get dinamically images from like this.
String imageId=yourImageId;
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier(imageId, "drawable", getPackageName()));
I think you should try this..
int photoId=R.drawable.move1;
img.setImageResource(photoId);

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.

Drawable resource using a variable

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());

Categories

Resources