从相机或图库在sqlite上保存图像

时间:2011-12-02 16:57:19

标签: android image sqlite camera imageview

我正在努力在sqlite上保存图像。 我想在sqlite上将图像保存为blob。 我不知道如何将imageView声明为byte[],因为我在dbAdapter上使用了插入方法。 是将图像保存到数据库中的好方法吗?有人说将文件路径url保存到数据库中更好。如果它更好,请给我一些你的手。 我真的很难过。 非常感谢。

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
     if (resultCode != RESULT_OK) return;
       switch (requestCode)
       {
        case PICK_FROM_CAMERA:
        Bundle extras = data.getExtras();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Bitmap selectedImage = (Bitmap) extras.get("data");
        selectedImage = Bitmap.createScaledBitmap(selectedImage, 200, 250, false);
        selectedImage.compress(CompressFormat.PNG, 0, bos);
        mImageView.setImageBitmap(selectedImage);
        break;

        case PICK_FROM_GALLERY:
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            mImageView.setImageURI(selectedImageUri);
        break;
       }
}

protected void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    saveState();
}

private void saveState() 
{
    String name = (String) nameEdit.getText().toString();
    String category = (String) categoryEdit.getText().toString();
    String expired_date = (String) expired_Date_Btn.getText().toString();
    //byte[] image = (byte[]) mImageView... I HAVE TO DO SOME CODING HERE.. 
    if(mRowId == null)
    {
        long id = mDbHelper.insertItem(category, name, expired_date, image);

        if(id>0)
        {
            mRowId = id;
        }           
    }
    else 
    {
        mDbHelper.updateItem(mRowId, category, name, expired_date, image);
    }   
}

DbAdapter类

    public long insertItem(String category, String name, String expired_date, byte[] image)
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_CATEGORY, category);
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_EXPIRED_DATE, expired_date);
    initialValues.put(KEY_IMAGE, image);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

2 个答案:

答案 0 :(得分:2)

关于相机的其他部分问题,这段代码将会有所帮助。

buttonClick = (Button) findViewById(R.id.buttonClick);
buttonClick.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
    }
});

myDBHelper = new MyDBHelper(this);
readDatabase();
}

protected void newTab(String label, Drawable icon, int page) {
    TabSpec tabSpec = tabHost.newTabSpec(label);
    tabSpec.setIndicator(label,icon);
    tabSpec.setContent(page);
    tabHost.addTab(tabSpec);
}

// Called when shutter is opened
ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {
    }
};

// Handles data for raw picture
PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
    }
};

// Handles data for jpeg picture
PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {

        SQLiteDatabase db = myDBHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("image", data);
        db.insert("storedImages", "tag", values);
        preview.camera.startPreview();
    }
};

protected void readDatabase() {
    TextView info = (TextView) findViewById(R.id.info);
    info.setText("Integer.toString(cursor.getCount())");
    SQLiteDatabase db = myDBHelper.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM storedImages ;", null);

    info.setText(Integer.toString(cursor.getCount()));

    if (cursor.getCount()>0) {
        cursor.moveToNext();
        ImageView image = (ImageView) findViewById(R.id.image);
        byte[] data = cursor.getBlob(cursor.getColumnIndex("image"));
        image.setImageBitmap(BitmapFactory.decodeByteArray(data, 0, data.length));
    }
}

}

Have a look at this

答案 1 :(得分:1)

尝试这样做:

ConvertDrawableToByteArray(mImageView.getDrawable()

使用ConvertDrawabelToByteArray定义如下:

public static byte[] ConvertDrawableToByteArray(Drawable drawableResource) {

    Bitmap imageBitmap = ((BitmapDrawable) drawableResource).getBitmap();
    ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream();
    imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, imageByteStream);
    byte[] imageByteData = imageByteStream.toByteArray();
    return imageByteData;
}

虽然我认为你应该能够从Bitmap中获取字节数。总而言之,将文件路径存储在sql lite中可能会更有效,但是,如果用户删除了您在应用程序中必须处理的图像。