简体   繁体   English

从相机捕获图像并将其存储在数据库中

[英]capture image from camera and store in database

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..... 我想将图像存储到从相机捕获的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);
    }
}

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 关于数据存储的好教程: 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 然后在必要时保存到数据库的路径: 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 或简单地进入共享首选项: http : //developer.android.com/training/basics/data-storage/shared-preferences.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM