如何在Android的图像和数据库工作? [英] how to work with images and database in android?
问题描述
我有关于存储在所述可绘制(多个)文件夹的图像的快速问题并具有与图像路径的表的数据库。我使用SQLite和我的图片png格式扩展。我怎样才能使其与我的数据库有一个图像路径和R.drawable工作?我怎样才能更好地利用我的形象的路径和assing相应的图像到相应的记录?
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?
例如:
在我的数据库,我有一个表称为甲板和列存储不是图像的完整路径,但一个局部路线中的一个。事情是这样的:
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)
在Allimages文件夹存储提拉 - LDPI文件夹中。顺便说一句,我在表6000唯一的记录。什么是这个问题的最好的方法呢?
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?
推荐答案
的Allimages文件夹存储提拉 - LDPI文件夹中。
结果
这就是无法。你不能在绘制文件夹的子文件夹。
the Allimages folder is stored within the drawable-ldpi folder.
This is not possible. You can't have subfolders in the drawable folders.
存储图像,如:
- lod_blueeyes.png
- dk1_darkhole.png
等。
然后,我只存储在我的数据库图像名称(没有扩展名)。
然后,我会使用一个函数从绘制文件夹检索图片:
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()
)
);
这篇关于如何在Android的图像和数据库工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!