0

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);
    }
}
}
4

1 回答 1

0

Intentandroid.provider.MediaStore.ACTION_IMAGE_CAPTURE不会在其“数据”Extra 中返回字节数组。它返回一个Uri必须打开的图像,例如

Uri uri = data.getData();

这提供了图像的内容路径,可以使用以下命令加载ContentResolver

InputStream input = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(input);

您需要确保检查Uri存在并优雅地处理错误,您永远不应该假设另一个应用程序会做一些明智的事情,并且应该尽可能地期待格式错误/丢失的数据。

于 2013-02-19T20:20:49.363 回答