如何在sqlite中存储图像

时间:2013-12-14 03:04:30

标签: android sqlite gridview

大家好我正在尝试开发一个项目,将数据库项目提供给gridview,所以我正在尝试几件事。现在我问的是如何使用我的代码填充我的gridview BTW即时通讯使用sqlite

这是我的databasehelper

// 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
}`

这是我的Items类

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,并且我在数据库中存储图像的过程是否正确?我已经有一个可绘制文件夹中的图像。

非常感谢任何帮助,谢谢你

1 个答案:

答案 0 :(得分:0)

保存对数据库中图像资源的引用是我处理它的方式。