Rakeeb Rajbhandari
Rakeeb Rajbhandari

Reputation: 5063

Camera Intent not working on lower android versions

On testing the camera intent class, my attempt to set the imageView in accordance with the photo clicked by the users failed miserably(the imageView did not display the taken image) on devices lower than Android 3.0. The class works fine in the latter Android devices.

Are there any changes required in this code so that devices lower than Android 3.0 can support this project too?

Initial Definitions

    private static final int TAKE_PICTURE = 0;
    private Uri mUri;
    private Bitmap mPhoto;

button.onClick Method:

    Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
    File f = new File(Environment.getExternalStorageDirectory(),  "photo.jpg");
    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
    mUri = Uri.fromFile(f);
    startActivityForResult(i, TAKE_PICTURE);

finally the onActivityResult:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                getContentResolver().notifyChange(mUri, null);
                ContentResolver cr = getContentResolver();
                try {
                    mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
                    ((ImageView)findViewById(R.id.photo_holder)).setImageBitmap(mPhoto);
                } catch (Exception e) {
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

Upvotes: 0

Views: 163

Answers (1)

Md. Monsur Hossain Tonmoy
Md. Monsur Hossain Tonmoy

Reputation: 11085

You should save the uri in onSaveInstanceState. As old device has low memory,when they start new app like camera they stop your application. After camera's activity is finished then it again recreate your activity from the saved state.

@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);

    if (mUri!= null) {

        outState.putString("cameraImageUri", mUri.toString());

    }

}

and in your onCreate do something like this

if (savedInstanceState != null) 
{
  mUri=Uri.parse(savedInstanceState.getString("cameraImageUri"));       
}

Upvotes: 1

Related Questions