Android 如何在sqlite中存储图像
大家好,我正在尝试开发一个项目,将数据库项放到gridview中,所以我尝试了几种方法。现在我要问的是如何使用我的代码填充我的gridview 顺便说一句,我使用sqlite 这是我的数据库助手Android 如何在sqlite中存储图像,android,sqlite,gridview,Android,Sqlite,Gridview,大家好,我正在尝试开发一个项目,将数据库项放到gridview中,所以我尝试了几种方法。现在我要问的是如何使用我的代码填充我的gridview 顺便说一句,我使用sqlite 这是我的数据库助手 // Database Name private static final String DATABASE_NAME = "SMART_CART"; // Item table name private static final String TBL_ITEMS = "tblItems"; // I
// Database Name
private static final String DATABASE_NAME = "SMART_CART";
// Item table name
private static final String TBL_ITEMS = "tblItems";
// Item Table Columns names
private static final String ITEM_BARCODE = "Barcode_ID";
private static final String ITEM_NAME = "Item_Name";
private static final String ITEM_PRICE = "Item_Price";
private static final String ITEM_CATEGORY = "Item_Category";
private static final String ITEM_IMG = "Item_IMG";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TBL_ITEMS + "("
+ ITEM_BARCODE + " INTEGER PRIMARY KEY," + ITEM_NAME + " NVARCHAR(255),"
+ ITEM_PRICE + " REAL," + ITEM_CATEGORY + " TEXT,"
+ ITEM_IMG + "BLOB" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TBL_ITEMS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new item
void addItems(Items items) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ITEM_BARCODE, items.getBarcode()); // Barcode
values.put(ITEM_NAME, items.getName()); // Item name
values.put(ITEM_PRICE, items.getPrice()); // Item Price
values.put(ITEM_CATEGORY, items.getCategory()); // Item Category
values.put(ITEM_IMG, items.getImage()); // Item Image
// Inserting Row
db.insert(TBL_ITEMS, null, values);
db.close(); // Closing database connection
}`
这是我的项目类
public class Items {
public Items() {
}
public Items(int barcode, String name, float price, String category,
String image) {
super();
this.barcode = barcode;
this.name = name;
this.price = price;
this.category = category;
this.image = image;
}
int barcode;
String name;
float price;
String category;
String image;
public int getBarcode() {
return barcode;
}
public void setBarcode(int barcode) {
this.barcode = barcode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
现在我的问题是如何用数据库中的所有数据填充gridview,以及在数据库中存储图像的过程是否正确?我已经在其中一个drawable文件夹中有了图像
如果您有任何帮助,我将不胜感激。提前谢谢您我会以这种方式处理数据库中对图像资源的引用 你能给我一个样品吗?如果你能给我一些。。。Tia有很多关于它的例子…一旦谷歌它???@kalyanpvs是的,我已经搜索过了,但我似乎找不到答案。你能举个例子吗?我真的是一个新手,我正在努力学习android。可能是重复的,而《大书呆子农场》的android书让你创建了一个应用程序,可以做一些你想要的事情。您可以从Flickr下载位图并在GridView中显示它们。在该应用程序和书中的其他应用程序中,有很多好东西需要学习。