我将位图转换为字节数组并尝试保存在sqlite数据库中,然后检索字节数组并在Bitmap中转换字节数组。
问题是当程序试图获取“c.getBlob(0)”的信息时,我得到一个异常。它也不适用于“byte [] b = c.getBlob(0)”。
可能的原因:
文件太大了? (1.2MB) 我必须做一些其他事情来配置它的BLOB类型? 谢谢!
public void saveImage(byte[] imageBitmap) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
Log.d("Size of imageBitmap saveImage : ",String.valueOf(imageBitmap.length));
values.put(KEY_IMAGE, imageBitmap);
db.insert(DATABASE_TABLE_IMAGE, null, values);
db.close();
}
public byte[] getImage(){
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery ="SELECT * FROM "+ DATABASE_TABLE_IMAGE +" WHERE "+ KEY_ID + " = "+ 1;
Cursor c = db.rawQuery(selectQuery, null);
if(c!=null){
try{
if(c.moveToFirst()){
Log.d("Size of imageBitmap getImage :",String.valueOf(c.getBlob(c.getColumnIndex(KEY_IMAGE)).length));
byte[] b=c.getBlob(c.getColumnIndex(KEY_IMAGE));
c.close();
return b;
}
}catch (Exception e) {
System.out.println(e.toString());
}
}
return null;
}
// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
// convert from byte array to bitmap
public static Bitmap getPhoto(byte[] image) {
ByteArrayInputStream imageStream = new ByteArrayInputStream(image);
Bitmap theImage= BitmapFactory.decodeStream(imageStream);
return theImage;//BitmapFactory.decodeByteArray(image, 0, image.length);
}