在我的應用我試圖拍攝圖像,並在ImageView的顯示它不加載,圖像被拍攝並添加到ImageView的但不被存儲或加載到SQLite的。我不知道爲什麼圖像不會存儲,因爲它加載到ImageView中。圖片來自SQLite的
代碼,以形象:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Take a picture and pass the image along to onActivityResult
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
startActivityForResult():
static final int REQUEST_IMAGE_CAPTURE = 1;
callingActivity = (MainActivity) getActivity();
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
// Get the photo
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap) extras.get("data");
imageView.setImageBitmap(photo);
// Convert to byte array
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// Add image to db here
(callingActivity.dbManager).addImageToRecipe(byteArray, recipe);
}
}
addImageToRecipe()是我的DBManager類中的方法:
public void addImageToRecipe(byte[] image, Recipe recipe) {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
try {
ContentValues values = new ContentValues();
values.put(COLUMN_IMAGE_ID, image);
db.update(TABLE_RECIPES, values, COLUMN_RECIPE_NAME + "=\"" + recipe.getRecipeTitle() + "\"", null);
} finally {
db.endTransaction();
}
db.close();
}
設置ImageView的( getImageId返回一個字節[])。它在我運行它的所有時間裏都不會進入if語句,這表明該值爲空。
if (recipe.getImageId() != null) {
Bitmap bmp = BitmapFactory.decodeByteArray(recipe.getImageId(), 0, recipe.getImageId().length);
imageView.setImageBitmap(bmp);
} else {
}
林不知道該怎麼做來解決它得到它的圖像加載到數據庫,然後從它放回ImageView的。