如何从sqlite android插入图像

时间:2014-06-19 10:19:59

标签: android image sqlite image-processing

我的应用程序中还有另一个问题...

这里的情景: 1.我有sqlite文件,我把它放在资产文件夹中 它包含标题:VARCHAR和图像:?? (此时我将使用VARCHAR问题)

  1. 我有存储在res / drawable文件夹中的图像

  2. 我在sqlite中设置了值,所以我的数据库已经包含一些值(标题和图像)

  3. 我的问题是

    是否可以从我的sqlite设置图像值,以便我可以从res / draweable文件夹中获取图像?

    如果可能,我应该使用什么类型的数据?以及如何在加载数据库时调用它

    如果不可能?什么是使用图像的最佳方式?

    这时我的sqlite助手

    public ArrayList<DrawerItem> getItemMenu(){
    
        ArrayList<DrawerItem> listMenu = new ArrayList<DrawerItem>();
    
        String selectQuery = "SELECT * FROM tbMenu";
    
        Cursor cursor = myDataBase.rawQuery(selectQuery, null);
        if(cursor.moveToFirst()){
            do{
                DrawerItem setMenu = new DrawerItem(selectQuery, Integer.parseInt(selectQuery));
                setMenu.setTitle(cursor.getString(0));
                setMenu.setImgResID(cursor.getInt(1));
    
                listMenu.add(setMenu);
            }while (cursor.moveToNext());
        }
    
        return listMenu;
    }
    
    你的时间让人们阅读我的问题...

1 个答案:

答案 0 :(得分:0)

你必须使用“blob”存储图像。

例如:将图像存储到db

public void insertImg(int id , Bitmap img ) {   


byte[] data = getBitmapAsByteArray(img); // this is a function

insertStatement_logo.bindLong(1, id);       
insertStatement_logo.bindBlob(2, data);

insertStatement_logo.executeInsert();
insertStatement_logo.clearBindings() ;

}

public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, outputStream);       
return outputStream.toByteArray();

}

从db

中检索图像
public Bitmap getImage(int i){

String qu = "select img  from table where feedid=" + i ;
Cursor cur = db.rawQuery(qu, null);

if (cur.moveToFirst()){
    byte[] imgByte = cur.getBlob(0);
    cur.close();
    return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
}
if (cur != null && !cur.isClosed()) {
    cur.close();
}       

return null ;
}