2016-10-06 11 views
-1

Speichern von Bitmap in die Datenbank einfügen:Bild kann nicht in db 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 Abrufen und in Bitmap-Umwandlung dann in Image zeigt:

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

Antwort

1

Verwendung dieser Methode:

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

Danke, seine Arbeit mit mich –