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.
Related
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"));
I have store all my images at the drawable folder in my app. How do i store the path of the image in my drawable folder to database?
db.addWords(new Words(1,"apple","applepathdrawable" ,1));
How do you put in that "applepathdrawable"?
Sorry it's my first time using database on android. Currently using SQLiteDatabase..
I recomend you to save the name of your resource as a plain String and get the ID dinamically using a method like this:
public static Integer getImageId(String name, Context context) {
if (name == null) {
return null;
} else {
return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
}
}
This sample is for your drawable folder.
For instance, if your want to get the ID for your drawable "apple.png", store in your DB "apple" and give this String as input to your method getImageId avobe defined.
I hope this would help you!
I have a quick question regarding to images stored in the drawable(s) folder and a database that has a table with image path. I am using sqlite and my images are png extension. How can I make it work with my database that has an image path and the R.drawable? How can I make a better use of my image path and assing the corresponding image to the corresponding record?
for example:
In my database, I have a table called deck and one of the column stores not a full path of the image but a partial path. Something like this:
Deck
---------------
/Allimages/lod/blueeyes.png
/Allimages/dk1/darkhole.png
.
.
.
( to N record)
the Allimages folder is stored within the drawable-ldpi folder. By the way, I have 6000 unique records in the table. What is the best approach for this problem?
the Allimages folder is stored within the drawable-ldpi folder.
This is not possible. You can't have subfolders in the drawable folders.
Store the images in a drawable folder like:
lod_blueeyes.png
dk1_darkhole.png
and so on.
Then, I'd store only the image names (without extension) in my db.
Then, I'd use a function to retrieve the images from the drawable folder:
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;
}
}
Usage: To retrieve the correct image
myImageView.setBackgroundResource
(
getResourceID
(
"my_image_name", "drawable", getApplicationContext()
)
);
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.
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)