I Want to store image into SQLite database Which Captured from camera ..this my code for capture image from camera after i struct to store that images into data-base.....
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);
}
}
you have to use "blob" to store images(bitmap).
db.execSQL("CREATE TABLE Funny(picid TEXT,myimage BLOB,gender TEXT,country TEXT)");
insert data using this:
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");
}
eg: to store an image in to db
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();
}
and to retrieve an image from db
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 ;
}
storing images into database is not a good idea, just store it to external storage and maybe save a link into your database.
Good tutorial on data-storage: http://developer.android.com/training/basics/data-storage/files.html
then save the path to your database if necessary: http://developer.android.com/training/basics/data-storage/databases.html
or simply into the shared preferences: http://developer.android.com/training/basics/data-storage/shared-preferences.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.