2016-10-06 81 views
-1

保存位圖導入數據庫:無法插入圖像插入分貝SQLITE

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+"');"); 

檢索blob和轉換成位圖,則表示它的ImageView:

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(); 

回答

1

使用這種方法:

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); 
} 
+0

謝謝,它的工作我 –