2015-08-26 62 views
-1

我在android的SQLite数据中保存了一些数据。我想显示或检索图像数据。以下是我正在尝试的代码。请帮我出:如何从SQLite数据库中检索图像

---检索图像,但我不能这样做 公共字符串的getData(){

// TODO Auto-generated method stub 

    String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_NUMBER }; 

    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 

    String result = ""; 

    int iRow = c.getColumnIndex(KEY_ROWID); 

    int iname = c.getColumnIndex(KEY_NAME); 

    // int iphoto = c.getColumnIndex(KEY_PHOTO); 

    int inumber = c.getColumnIndex(KEY_NUMBER); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 

     result = result + c.getString(iRow) + " " + c.getString(iname) + " " + c.getString(inumber) + "\n"; 

    } 

    return result; 
} 

----这种方法节省了SQLite数据库数据: 众长create_Entry (ContactInfo objContact){

// TODO Auto-generated method stub 

    ContentValues cv = new ContentValues(); 

    cv.put(KEY_NAME, objContact.getFname()); 

    cv.put(KEY_NUMBER, objContact.getPhoneNo()); 

    cv.put(KEY_PHOTO, Utility.getBytes(objContact.getBitmap())); 

    return ourDatabase.insert(DATABASE_TABLE, null, cv); 




} 

回答

0

在插入数据库之前,您需要先将位图图像转换为字节数组,然后再将其应用于数据库查询。 当从数据库中检索时,你肯定有一个字节数组的图像,你需要做的是将字节数组转换回原始图像。

代码片断 -

// convert from bitmap to byte array 
    public static byte[] getBytes(Bitmap bitmap) { 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(CompressFormat.PNG, 0, stream); 
     return stream.toByteArray(); 
    } 

    // convert from byte array to bitmap 
    public static Bitmap getImage(byte[] image) { 
     return BitmapFactory.decodeByteArray(image, 0, image.length); 
    } 
+0

我已经有这个在我的代码。但我在从SQLite数据库检索图像时出错。 –

+0

您需要在'Byte array' byte []中获取图像image = cursor.getBlob(1); – ryderz8