0

I am trying to find the best way to implement a button in a MainActivity that launches the camera activity and returns the image to another activity. basically it returns the image to an activity to add a description to the image... what i thought would be a good idea is to start that single photo view activity when the camera button is pressed and from the new activity start the camera activity for result before doing anything else; but i have an intermittent issue that sometimes the camera gets stuck in a loop.

maybe there is a better approach? should i launch the camera activity for result first and then pass the image as an intent?

here is what i have now:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }else if ( id == R.id.menu_rules ){
        Intent rulesIntent = new Intent(this, RulesActivity.class);
        startActivity(rulesIntent);
        return true;
    }else if ( id == R.id.menu_import_photo ){
        //pull in an image from the gallery

        return true;
    }else if ( id == R.id.menu_item_new_photo ){
        startActivity(new Intent(MainActivity.this, SinglePhotoViewActivity.class));
    }
    return super.onOptionsItemSelected(item);
}

and the receiving activity..

public class SinglePhotoViewActivity extends Activity {

private static final String LOG_TAG = "Simple Camera App";
private static int TAKE_PICTURE = 1001;
private ImageView imageView;
private Uri imageUri;
private Bitmap bitmap;
private TextView textView;
private EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_photo_view);

    if ( bitmap == null )
        takePhoto();


}

public void takePhoto(){
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment
            .getExternalStoragePublicDirectory(Environment
                    //this will continually overwrite the same file
                    //need to implement a date/ time string
                    //to save as file name.
                    .DIRECTORY_PICTURES), "pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
        case 1001:
            if(resultCode == Activity.RESULT_OK) {
                getContentResolver().notifyChange(imageUri, null);
                imageView = (ImageView )findViewById(R.id.ivCameraImageView);
                ContentResolver contentResolver = getContentResolver();

                try {
                    bitmap = android.provider.MediaStore.Images.Media.getBitmap(contentResolver,
                        imageUri);
                    imageView.setImageBitmap(bitmap);
                } catch(Exception e) {
                    Toast.makeText(SinglePhotoViewActivity.this, "failed to load",
                             Toast.LENGTH_LONG).show();
                    Log.e(LOG_TAG, e.toString());
                }
            }
    }
}
}
StillLearningToCode
  • 2,271
  • 4
  • 27
  • 46

1 Answers1

1

Your situation is similar to what happens often when ACTION_IMAGE_CAPTURE intent is launched (see e.g. Android startCamera gives me null Intent and ... does it destroy my global variable?).

The cause is that the system may decide to destroy the caller activity (in your case, the SinglePhotoViewActivity), and recreate it again after the launched activity (in your case, system Camera activity) returns the result.

The ultimate fix involves implementing onSaveInstanceState(Bundle) in the calling activity. But in your specific situation, there is a shortcut. Check the intent in onCreate(), and if it comes from the MainActivity, then launch Camera. Else, proceed as if you are going to receive onActivityResult() right now.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307