Java Can';t将图像插入db sqLite
将位图保存到数据库:Java Can';t将图像插入db sqLite,java,android,sqlite,bitmap,android-sqlite,Java,Android,Sqlite,Bitmap,Android Sqlite,将位图保存到数据库: Bitmap photo = value.getParcelableExtra("Bitmap"); if (photo != null) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.PNG, 100, bos); bArray = bos.toByteArray();
Bitmap photo = value.getParcelableExtra("Bitmap");
if (photo != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
bArray = bos.toByteArray();
}
// photo_imi.setImageBitmap(photo);
//textview1.setText("Your name is: " + user_name + " AND Your email is " + user_email);
SQLiteDatabase db=openOrCreateDatabase("MyDataBas", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS USER_INFO (name VARCHAR, email VARCHAR, imageCol BLOB);");
//ContentValues cv=new ContentValues();
//cv.put("name", user_name);
//cv.put("email", user_email);
//cv.put("img", bArray);
//db.insert("USER_INFO", null, cv);
db.execSQL("INSERT INTO USER_INFO ('name','email','imageCol')VALUES('"+user_name+"','"+user_email+"','"+bArray+"');");
String query="SELECT * FROM USER_INFO WHERE TRIM(name) = '" + user_name.trim() +"'";
Cursor detail= db.rawQuery(query,null);
if(detail!=null){
detail.moveToFirst();
do{
img=detail.getBlob(2);
s_email=detail.getString(detail.getColumnIndex("email"));
fimg= BitmapFactory.decodeByteArray(img, 0, img.length);
}while(detail.moveToNext());
}
show_email=(TextView) findViewById(R.id.show_email);
show_email.setText("Email:" + s_email);
fetch_image = (ImageView) findViewById(R.id.fetch_image);
fetch_image.setImageBitmap(fimg);
Toast.makeText(this, "Retrive successfully", Toast.LENGTH_SHORT).show();
检索blob并将其转换为位图,然后在ImageView中显示:
Bitmap photo = value.getParcelableExtra("Bitmap");
if (photo != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
bArray = bos.toByteArray();
}
// photo_imi.setImageBitmap(photo);
//textview1.setText("Your name is: " + user_name + " AND Your email is " + user_email);
SQLiteDatabase db=openOrCreateDatabase("MyDataBas", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS USER_INFO (name VARCHAR, email VARCHAR, imageCol BLOB);");
//ContentValues cv=new ContentValues();
//cv.put("name", user_name);
//cv.put("email", user_email);
//cv.put("img", bArray);
//db.insert("USER_INFO", null, cv);
db.execSQL("INSERT INTO USER_INFO ('name','email','imageCol')VALUES('"+user_name+"','"+user_email+"','"+bArray+"');");
String query="SELECT * FROM USER_INFO WHERE TRIM(name) = '" + user_name.trim() +"'";
Cursor detail= db.rawQuery(query,null);
if(detail!=null){
detail.moveToFirst();
do{
img=detail.getBlob(2);
s_email=detail.getString(detail.getColumnIndex("email"));
fimg= BitmapFactory.decodeByteArray(img, 0, img.length);
}while(detail.moveToNext());
}
show_email=(TextView) findViewById(R.id.show_email);
show_email.setText("Email:" + s_email);
fetch_image = (ImageView) findViewById(R.id.fetch_image);
fetch_image.setImageBitmap(fimg);
Toast.makeText(this, "Retrive successfully", Toast.LENGTH_SHORT).show();
使用此方法:
public void InsertToDB( String name, String email, byte[] image) throws SQLiteException {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("email", email);
cv.put("imageCol", image);
database.insert("USER_INFO ", null, cv);
}