这是我的数据库的图像,你可以在这里看到图像插入成功的字节格式。 这种数据库结构
public void onCreate(SQLiteDatabase db) {
final String[] creatStatments = new String[]{"create table "
+ Image_handler
+ "(_id INTEGER PRIMARY KEY AUTOINCREMENT,"+ Image_handeler_column+" BLOB)"
};
我使用以下代码从数据库中检索图像
System.out.println(" satrt image rertrive");
cursor = dh.rawQuery("SELECT _id, image_byte FROM image_database",null);
int i=0;
if (cursor.moveToFirst()) {
do {
String str_cursor =cursor.getString(1);
System.out.println("string value of cursor"+str_cursor);
// get the data into array,or class variable
bb = cursor.getBlob(cursor.getColumnIndex(DatabaseHelper.Image_handeler_column));
System.out.println("byte array value"+bb);
i++;
} while (cursor.moveToNext());
Bitmap bmp=BitmapFactory.decodeByteArray(bb,0,bb.length);
System.out.println("bitmap value"+bmp);
imageView.setImageBitmap(bmp);
}
当我在上面工作时,字节数组值的代码值显示为B @ 405889a8,而bmp(Bitmap)值对我来说是空的。所以为什么我得到bmp值为null。 我想更新我的问题。为了从url在数据库中插入图像,我正在使用以下代码
static private Bitmap downloadBitmap(String url) throws IOException {
HttpUriRequest request = new HttpGet(url);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
System.out.println("kjklcmklxc");
HttpEntity entity = response.getEntity();
byte[] bytes = EntityUtils.toByteArray(entity);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
bytes.length);
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byt_aary_outpt_strm);
dh.delete(DatabaseHelper.Image_handler, null, null);
bitmapdata = byt_aary_outpt_strm.toByteArray();
System.out.println("bitmap of image converted image");
for(int i =0 ; i<bitmapdata.length;i++){
convert_save_byte_str = convert_save_byte_str+bitmapdata[i];
}
System.out.println("njdsfnh"+convert_save_byte_str);
ContentValues userdetailValues = new ContentValues();
userdetailValues.put("image_byte", convert_save_byte_str);
System.out.println("between put and insert");
dh.insert(DatabaseHelper.Image_handler, null, userdetailValues);
cursor = dh.rawQuery("SELECT _id, image_byte FROM image_database",null);
int i=0;
if (cursor.moveToFirst()) {
do {
// get the data into array,or class variable
bb = cursor.getBlob(cursor.getColumnIndex(DatabaseHelper.Image_handeler_column));
//System.out.println("productid"+data);
//intent.putExtra("product_id", data);
System.out.print("bytengkfgkjgk"+bb[i]);
i++;
} while (cursor.moveToNext());
}
return bitmap;
} else {
throw new IOException("Download failed, HTTP response code "
+ statusCode + " - " + statusLine.getReasonPhrase());
}
}