我很努力地在sqlite上保存圖像。 我想將圖像保存爲sqlite上的blob。 我不知道如何聲明imageView
到byte[]
,因爲我在dbAdapter
上使用插入方法。 將圖像保存到數據庫是否好方法?有人說將文件路徑url保存到數據庫中會更好。如果更好,請給我一些你的手。 我真的越來越難。 非常感謝。從相機或圖庫在sqlite上保存圖像
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);
}
感謝您的回覆,這是爲我好。但是,仍然我的應用程序不能處理數據庫與圖像:( – wholee1
你可以展開你的意思是它不會處理圖像? – Chris
我認爲DbAdapter有問題,因爲圖像列無法得到它。在dbAdapter中,我創建圖像列爲blob類型,但是,sqlite目前無法獲得blob列,我不知道它是什麼問題,當我運行應用程序時,它不能從數據庫中獲取項目,然後我仍然在爲保存圖像而苦苦掙扎給出了答案.. – wholee1