我正在尝试创建一个应用程序,我需要在SQLite数据库中存储和检索图像。我得到的程序使用BLOB将图像存储为字节数组,我也能够检索字节数组,但在将数组解码为位图时,BitmapFactory返回null。请帮忙。 这是我用来解码数组的代码。
Cursor c=db.rawQuery("SELECT * FROM student WHERE name='"+"1"+"'", null);
if(c.moveToFirst())
{
byte[] outImage=c.getBlob(1);
Log.d("The out image is", String.valueOf(outImage));
Bitmap bitmap = BitmapFactory.decodeByteArray(outImage , 0, outImage .length);
iv.setImageBitmap(bitmap);
}
这就是我在数据库中插入byte []的方法:
if(requestCode==CAMERA_REQUEST && resultCode==RESULT_OK){
Bitmap yourImage = (Bitmap) data.getExtras().get("data");
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
Log.e("output before conversion", imageInByte.toString());
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.execSQL("INSERT INTO student VALUES('"+"1"+"','"+imageInByte+"');");
Log.d("Success: ", "Image Saved");
}
logcat读取类似这样的内容
12-24 23:39:49.777: E/output before conversion(22523): [B@13fd7069
12-24 23:39:49.778: D/Insert:(22523): Inserting ..
12-24 23:39:49.840: D/Success:(22523): Image Saved
12-24 23:39:51.579: D/The out image is(22523): [B@155f0aee
12-24 23:39:51.580: D/skia(22523): --- SkImageDecoder::Factory returned null
答案 0 :(得分:0)
解码器返回null
,因为您没有传递有效数据。
首先将blob
转换为byteArray
Blob blob = c.getBlob(1);
int blobLength = (int) blob.length();
byte[] outImage = blob.getBytes(1, blobLength);
Log.d("The out image is", String.valueOf(outImage));
Bitmap bitmap = BitmapFactory.decodeByteArray(outImage , 0, outImage .length);
iv.setImageBitmap(bitmap);
blob.free();