2013-12-08 35 views
0

我想將圖像存儲到SQLite數據庫從攝像頭捕獲..這是我的代碼從攝像頭捕獲圖像後,我結構將該圖像存儲到數據庫.....從相機捕捉圖像和存儲在數據庫

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.photobtn: 
     i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(i, camaradata); 
     break; 
    case R.id.submit: 
     break; 
    } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     bmp = (Bitmap) extras.get("data"); 
     imageview.setImageBitmap(bmp); 
    } 
} 
+1

將圖像存儲到數據庫不是一個好主意,只是將其存儲到外部存儲,也許保存一個鏈接到您的數據庫... – bbuecherl

+0

@mindsonic好..我同意你..但你可以幫我,因爲如何存儲圖像在外部存儲。 – bhavdip

回答

0

你必須使用「blob」來存儲圖像(位圖)。使用這種

db.execSQL("CREATE TABLE Funny(picid TEXT,myimage BLOB,gender TEXT,country TEXT)"); 

插入數據:

public void insert(String string,byte[] bytes,String gender,String country) { 
ContentValues cv=new ContentValues(); 
cv.put("picid",string); 
cv.put("myimage",bytes); 
cv.put("gender", gender); 
cv.put("country", country); 
getWritableDatabase().insert("Funny","gender", cv); 
Log.e("inserted", "inserted"); 
} 

例如:到的圖像存儲在到分貝

public void insertImg(int id , Bitmap img) { 


    byte[] data = getBitmapAsByteArray(img); // this is a function 

    db.insert("01",data,"male","Pakistan"); 

} 

public static byte[] getBitmapAsByteArray(Bitmap bitmap) { 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    bitmap.compress(CompressFormat.PNG, 0, outputStream);  
    return outputStream.toByteArray(); 
} 

,並從分貝

public Bitmap getImage(int i){ 

    String qu = "select myimage from Funny where picid=" + i ; 
    Cursor cur = db.rawQuery(qu, null); 

    if (cur.moveToFirst()){ 
     byte[] imgByte = cur.getBlob(0); 
     cur.close(); 
     return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length); 
    } 
    if (cur != null && !cur.isClosed()) { 
     cur.close(); 
    }  

    return null ; 
} 
+0

我可以通過使用「contentvalue」對象插入這些數據.. ??以及如何.. ?? – bhavdip

0

存儲檢索圖象圖像到數據庫不是一個好主意,j最好將它存儲到外部存儲器中,也許將鏈接保存到數據庫中。

上的數據存儲很好的教程: http://developer.android.com/training/basics/data-storage/files.html

然後路徑保存到數據庫中,如果必要的: http://developer.android.com/training/basics/data-storage/databases.html

或者乾脆到共享偏好: http://developer.android.com/training/basics/data-storage/shared-preferences.html

相關問題