Android 如何从SQLite数据库中检索图像
我正在为android保存SQLite数据中的一些数据。我想显示或检索图像数据。下面是我正在尝试的代码。请帮帮我: ---正在检索图像,但我做不到 公共字符串getData(){ ----此方法正在SQLite数据库中保存数据: 公共长创建项(ContactInfo objContact){Android 如何从SQLite数据库中检索图像,android,sqlite,Android,Sqlite,我正在为android保存SQLite数据中的一些数据。我想显示或检索图像数据。下面是我正在尝试的代码。请帮帮我: ---正在检索图像,但我做不到 公共字符串getData(){ ----此方法正在SQLite数据库中保存数据: 公共长创建项(ContactInfo objContact){ 在插入数据库之前,您需要先将位图图像转换为字节数组,然后使用数据库查询应用它。 当从数据库中检索时,您肯定有一个图像的字节数组,您需要做的是将字节数组转换回原始图像 代码片段- // convert fr
在插入数据库之前,您需要先将位图图像转换为字节数组,然后使用数据库查询应用它。 当从数据库中检索时,您肯定有一个图像的字节数组,您需要做的是将字节数组转换回原始图像 代码片段-
// 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);
}
我的代码中已经有这个。但是我从SQLite数据库检索图像时出错。您需要在
字节数组字节[]image=cursor.getBlob(1)中获取图像;
// 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);
}
// 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);
}