How to change an ImageView with a filename - android

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

Related

Setting image variable in fragments

Sorry, I'm still a beginner in coding, but how do I make this code work? actor art is a variable I have from an array.
And it's the name of a image I have in my drawable. This line of code works on any activity type but not on fragments. How do I set image from a variable from an array?
int imageId = AppUtil.getImageIdFromDrawable(this, actorart);
ImageView ivCoverArt = view.findViewById(R.id.imgactor);
ivCoverArt.setImageResource(imageId);
The error for this is that this cannot be used.
This also doesn't work:
ImageView imageView= view.findViewById(R.id.imgactor);
imageView.setImageResource(actorart);
The error here is that actorart is a String but setImageResource is an action for an int.
You are correct, setImageResource() takes in an integer, more specifially the ID of the resource.
To get the ID, you can use the following:
int id = getResources().getIdentifier("yourpackagename:drawable/" + actorart, null, null);
this cannot be used cause the instance you are refering with this is a Fragment, and I'm pretty sure you need a Context parameter.
To fix it get a valid Context:
int imageId = AppUtil.getImageIdFromDrawable(getActivity(), actorart);

Android SharedPreferences get drawable if value doesn´t exist

so in my app I need to save and to display an image on a image view, so i use SharedPreferences for saving it, the problem is that, if there is not a saved value, i want to display an image from drawables, and for that I´m using this code:
final ImageButton imgView = (ImageButton) findViewById(R.id.UserImageButton);
String photo = PreferenceManager.getDefaultSharedPreferences(UserActivity.this).getString("Image", String.valueOf(getResources().getDrawable(R.drawable.user, getApplicationContext().getTheme())));//NameOfTheString, StringIfNothingFound
imgView.setImageBitmap(BitmapFactory.decodeFile(photo));
But if there is no image saved, the drawable is not used on the ImageView, and it doesn´t show anything.
How could I fix it?
There's no requirement to use the default value of your getString() method as a direct path to your drawable. You could simply do the following:
private static final String IMAGE_PREF = "image_pref_key";
private static final String PREF_FAIL_CODE= "no_image_found";
String photo = PreferenceManager.getDefaultSharedPreferences(UserActivity.this).getString(IMAGE_PREF, PREF_FAIL_CODE);
if (photo.equals(PREF_FAIL_CODE) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.user));
}
When you are getting the string from default shared preferences, if the value is not stored, set the default value returned to something e.g. "not found"
Now before setting doing setImageBitmap check if the value in your String variable photo is "not found". If it is display the drawable from your resource. Else display your saved image after decoding it as your are doing it currently.

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.

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.

How to set the image from drawable folder to imageview using the path of the image from sqlite database?

anyone knows how to set the image from drawable folder to imageview using the path of the image from sqlite database?
Any help will be appreciated. Sample codes will be helpful. Thanks. =)
ImageView imageView = (ImageView) findViewById(R.id.imageView);
String test = "imageName"; // store only image name in database(means not store "R.drawable.image") like "image".
int id = getResources().getIdentifier("projectPackageName:drawable/" + test, null, null);
imageView.setImageResource(id);
You can get Bitmap object from file,and use it to set your image view.
Here code:
String img_path = get from your database
...
Bitmap bmp = BitmapFactory.decodeFile(img_path);
//use it.
yourimgview.setImageBitmap(bmp);
^_^

Categories

Resources