我想在Android中的Image
数据库中保存Sqlite
。我希望在插入后在ImageView
上设置该图像。我尝试使用以下代码但图像未设置。任何人都可以告诉我代码的问题。
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Log.i("ic launcher byte array", ""+byteArray);
db.insert_signature_table(String.valueOf(AssignedJobs.jobId), String.valueOf(AssignedJobs.teamId), byteArray);
Log.i("get all signature data", ""+db.getAll_signature_table().get(0).get("sign"));
byte[] bb = db.getAll_signature_table().get(0).get("sign").getBytes();
Log.i("decodes", ""+bb);
Bitmap bmp1 = BitmapFactory.decodeByteArray(bb, 0, bb.length);
edt_signature.setImageBitmap(bmp1);
dbHelper类
query_signature_table = "CREATE TABLE signature_table(JobId STRING,TeamId STRING,sign BLOB)";
database.execSQL(query_signature_table);
public void insert_signature_table(String jobId, String teamId, byte[] imageInByte) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("JobId", jobId);
values.put("TeamId", teamId);
values.put("sign", imageInByte);
Log.i("values assigned", "" + imageInByte);
database.insert("signature_table", null, values);
database.close();
}
public ArrayList<HashMap<String, String>> getAll_signature_table() {
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM signature_table";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("JobId", cursor.getString(0));
map.put("RigId", cursor.getString(1));
byte[] temp_image = cursor.getBlob(2);
map.put("sign",""+temp_image);
wordList.add(map);
} while (cursor.moveToNext());
}
答案 0 :(得分:0)
在我的一个项目中,我已保存图像并从数据库中检索它,因此我与您共享该代码
public static byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
public static Bitmap getImageFromBytes(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
这些是我用来保存和检索数据库中的图像并将图像字段创建为数据库BLOB的方法
答案 1 :(得分:0)
这是工作。试试吧
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("JobId", cursor.getString(0));
map.put("RigId", cursor.getString(1));
byte[] temp_image = cursor.getBlob(2);
Bitmap bitmap = BitmapFactory.decodeByteArray(temp_image , 0, temp_image .length);
imageView.setImageBitmap(bitmap);
map.put("sign",""+bitmap);
wordList.add(map);
} while (cursor.moveToNext());