코딩이나 논리적 인 결함이 어디에서 잘못되었는지 알아 내려고하고, 두 개의 문자열과 이미지를 취하는 레서피 앱을 만들었습니다. 모든 데이터는 데이터베이스와 메인 화면 데이터베이스에서 조리법 목록을 얻습니다.안드로이드 이미지 비트 맵을 SQLite 블로브로 업데이트
작성 또는 예상대로 새로운 데이터가 작동 추가
메인 화면
추가/편집 화면은 모든 데이터가 저장하세요. 문제는 이미지가 두 번 시도되면 이미지에 영향을 미치지 않는 것처럼 저장되면 이미지를 제외하고 모든 것이 업데이트 될 수 있습니다. 이미지는 동일하게 유지됩니다.
핵심 원리는 (사용자가 활동을 시작에서 onCreate 또는 활동 onResume를 다시 시작하면)와 뷰에서 데이터를 얻을 수 (사용자 그냥 넣다 업데이트가되었다 onPause와 onSaveInstanceState을 떠날 때)
뷰에 데이터를 설정하는 것입니다코드
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
enableEdit();
fab.hide();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
recipeDAOImp = new RecipeDAOImp(this);
recipeDAOImp.open();
findViews();
rowId = (savedInstanceState == null) ? null :
(Long) savedInstanceState.getSerializable(RecipeDAOImp.KEY_ID);
if (rowId == null) {
Bundle extras = getIntent().getExtras();
rowId = extras != null ? extras.getLong(RecipeDAOImp.KEY_ID)
: null;
}
populateData();
disableEdit();
}
private void findViews() {
// Finds Views and Set onClick to imageButton
}
private void disableEdit() {
// Disable Views
}
private void enableEdit() {
// Enables Views
}
private void populateData() {
// If rowId is available then user is trying to Edit Recipe
if (rowId != null) {
setTitle("Edit Recipe");
Recipe recipe = new Recipe(rowId);
Cursor cursor = recipeDAOImp.getRecipe(recipe);
startManagingCursor(cursor);
title.setText(cursor.getString(
cursor.getColumnIndexOrThrow(RecipeDAOImp.KEY_TITLE)));
ingredients.setText(cursor.getString(
cursor.getColumnIndexOrThrow(RecipeDAOImp.KEY_INGREDIENTS)));
steps.setText(cursor.getString(
cursor.getColumnIndexOrThrow(RecipeDAOImp.KEY_STEPS)));
category.setText(cursor.getString(
cursor.getColumnIndexOrThrow(RecipeDAOImp.KEY_CATEGORY)));
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), DbBitmapUtility.getImage(cursor.getBlob(
cursor.getColumnIndexOrThrow(RecipeDAOImp.KEY_IMAGE))));
image.setBackground(bitmapDrawable);
// Else user is Adding a new Recipe
} else {
fab.hide();
setTitle("Add Recipe");
enableEdit();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
outState.putSerializable(RecipeDAOImp.KEY_ID, rowId);
}
@Override
protected void onPause() {
super.onPause();
saveState();
}
@Override
protected void onResume() {
super.onResume();
populateData();
}
private void saveState() {
// Get the values from the views
String titleString = title.getText().toString();
String ingredientString = ingredients.getText().toString();
String stepsString = steps.getText().toString();
String categoryString = category.getText().toString();
// Get the image from imageButton
Drawable drawable = image.getBackground();
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
byte[] imageData = DbBitmapUtility.getBytes(bitmap);
// Just to clarify image is never null as the backround is a camre image
if (titleString.equals(null) || "".equals(titleString) || ingredientString.equals(null) || "".equals(ingredientString) || stepsString.equals(null) || "".equals(stepsString) || categoryString.equals(null) || "".equals(categoryString) || imageData.equals(null) || "".equals(imageData)) {
Toast.makeText(this, "No Data Saved", Toast.LENGTH_SHORT).show();
} else {
Recipe recipe = new Recipe(titleString, ingredientString, stepsString, categoryString, imageData);
// If rowId is not Available then user is Creating a new Recipe
if (rowId == null) {
long id = recipeDAOImp.createRecipe(recipe);
if (id > 0) {
rowId = id;
}
} else {
recipe.setId(rowId);
recipeDAOImp.updateRecipe(recipe);
}
}
}
@Override
public void onClick(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
// Set the imageButton
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), imageBitmap);
image.setBackground(bitmapDrawable);
}
}
}
DAO
@Override
public boolean updateRecipe(Recipe recipe) {
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_TITLE, recipe.getTitle());
contentValues.put(KEY_INGREDIENTS, recipe.getIngredients());
contentValues.put(KEY_STEPS, recipe.getSteps());
contentValues.put(KEY_CATEGORY, recipe.getCategory());
contentValues.put(KEY_IMAGE, recipe.getImage());
return sqLiteDatabase.update(DATABASE_TABLE, contentValues, KEY_ID + "=" + recipe.getId(), null) > 0;
}
문자열 데이터를 업데이트 할 수 있지만 저장된 이미지를 실제로 업데이트 할 수없는 문제는 무엇입니까?
문제는 DAO 코드가 아니며, 업데이트 파트를 포함하도록 질문을 편집했습니다. 코드가 작동한다는 것입니다. 아직 작동하지 않습니다. 그것의 간단한 애플 리케이션은 모든 데이터가 모듈에서 뷰로, 그리고 뷰에서 모듈로 온다. 창조는 모두 좋지만 업데이트하지 않는다. 이미지는 일단 설정되면 변경되지 않는다. 아마도 제가 언급 한 것과는 다른 재 작성이나 다른 논리가 도움이 될 수 있습니다. –
번들에서 새 비트 맵을 가져 오는 방법을 변경해보십시오. Intent.getParcelableExtra (http://stackoverflow.com/a/12908133/678123)를 사용하거나 바이트 배열로 전달할 수 있습니다 (http://stackoverflow.com/a/11010565/678123). – Greg
아니요, 위의 코드를 보면 이전 활동에서 선택한 항목을 선택한 다음보기/편집 모드가있는 경우에만 ID에 관심이 있습니다. 선택/ID가 null이 아닌 경우 새 항목/작성 모드가 유일한 것입니다. 엑스트라를 지나가고있다.getLong (RecipeDAOImp.KEY_ID) 각 액티비티는 자신의 작업을 처리합니다. 모든 데이터는 모듈에서 뷰로, 뷰에서 모듈로 보내지 만 일단 이미지가 일단 DB에 저장된 후에는 변경되지 않습니다. 카메라에서 이미지를 가져 와서 imagebutton으로 설정할 수 있지만 이미지를 저장하지 않고 이미지를 저장하면 이미지는 이미 DB에서 가져옵니다. 귀하의 의견에 감사드립니다. –