I had idea to build simple app that can store text and images. I started from Notepad Tutorial (http://developer.android.com/training/notepad/index.html) from first and second exercise. After I did second I arrange exercise to handle images, it was working very well unless user tapped back button in device. With help came thid exercise where the goal to handling life-cycle events. I managed to did it, it is working very well for old entries in database, but when I try to add new Image, my ImageView is not filled with just taken photo. Can you spot where is the problem?
public class ParagonArmageddonEdit extends Activity
{
private EditText mTitleText;
private EditText mBodyText;
private Long mRowId;
private ImageView imageContainer;
private byte[] imageInByteArray;
private Button takePhotoButton;
private ParagonDbAdapter mDbHelper;
private static final long serialVersionUID = 1L;
public boolean classEnabled;
private static final int CAMERA_REQUEST = 1888;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mDbHelper = new ParagonDbAdapter(this);
mDbHelper.open();
setContentView(R.layout.paragonarmageddon_edit);
setTitle(R.string.edit_note);
this.imageContainer = (ImageView) this.findViewById(R.id.ImageView);
this.takePhotoButton = (Button) this.findViewById(R.id.photoButton);
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
Button confirmButton = (Button) findViewById(R.id.Save);
mRowId = (savedInstanceState == null) ? null :
(Long) savedInstanceState.getSerializable(ParagonDbAdapter.KEY_ROWID);
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(ParagonDbAdapter.KEY_ROWID)
: null;
}
populateFields();
this.takePhotoButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//camera Intent
Intent IntentKamery = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(IntentKamery, CAMERA_REQUEST);
}
});
confirmButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
setResult(RESULT_OK);
finish();
}
});
}
private void populateFields() {
if (mRowId != null) {
Cursor note = mDbHelper.fetchNote(mRowId);
startManagingCursor(note);
mTitleText.setText(note.getString(
note.getColumnIndexOrThrow(ParagonDbAdapter.KEY_TITLE)));
mBodyText.setText(note.getString(
note.getColumnIndexOrThrow(ParagonDbAdapter.KEY_BODY)));
byte[] photo = note.getBlob(note.getColumnIndexOrThrow(ParagonDbAdapter.KEY_ZDJECIE));
Bitmap photoConvertedToBitmap = BitmapFactory.decodeByteArray(zdjecie, 0, photo.length);
this.imageContainer.setImageBitmap(photoConvertedToBitmap);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
{
Bitmap photoConvertedToBitmap = (Bitmap) data.getExtras().get("data");
imageContainer.setImageBitmap(photoConvertedToBitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photoConvertedToBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
imageInByteArray = stream.toByteArray();
photoTaken = true;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
outState.putSerializable(ParagonDbAdapter.KEY_ROWID, mRowId);
}
@Override
protected void onPause() {
super.onPause();
saveState();
}
@Override
protected void onResume() {
super.onResume();
populateFields();
}
private void saveState() {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
imageContainer.buildDrawingCache();
Bitmap photoConvertedToBitmap = imageContainer.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photoConvertedToBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
imageInByteArray = stream.toByteArray();
if (mRowId == null) {
long id = mDbHelper.createNote(title, body, imageInByteArray);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateNote(mRowId, title, body, imageInByteArray);
}
}
}