how to work with images and database in android? - android

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

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 save image path/image to database where i have my images in my drawable folder?

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!

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

Store uri of raw folder's files into ArrayList<String>

I have 120 images that I have stored in raw folder. I did this on purpose . And please do not suggest me about putting those in drawable and so on. To store the uri of each file into an Arraylist of String I have used following way/method.
private static void getRawFiles() throws IllegalAccessException, IllegalArgumentException{
java.lang.reflect.Field[] fields = R.raw.class.getFields(); // fileds stores resid and
// loop for every file in raw folder
if (uriCollection.isEmpty()){
for (int count = 0; count < fields.length; count++) {
String filename ="android.resource://" +HomeActivity.PACKAGE_NAME + "/"+"R.raw."+ "_"+count;
uriCollection.add(filename);
Log.i("fileUri",filename);
}
}
The file name in raw folder starts with _0._01 and so on. So I have accessed this from another class and setImageURI to display images in the ImageView. But I have been getting following errors as well as application stops
- resolvedUri failed on bad bitmap
- unable to open content; filenotfoundexception
If you have any ideas or any solution or if you have come across such problem then i would like to kindly request for your assistance. Thanks in advance !
Can you work with Reource Ids? I usually do this to dynamically get at files.
In your loop - you will have to decide how you generate the imageName - generate your imageName and call:
String imageName = "_0.01";//OBSERVE it's not "_0.01.jpg"
int resId = context.getResources().getIdentifier(imageName, "raw", context.getPackageName());
aListOfResourceIDs.add(resId);

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.

Categories

Resources