1

I am new to Android programming and I'm writing an application in Java that opens the camera take a photo and save it. I made it via Intents but I can't see onActivityResult running.

I have tested it into my phone (Samsung Galaxy S) and when I take the photo I receive a preview of that photo having two buttons one Save and the other Cancel. I haven't added something to my code to do this so I think it's something that camera does. I want after capturing the image to run onActivityResult (after I press the Save button on the preview).

But how I'm going to return a result to start onActivityResult after pressing the Button Save on the preview?

I FORGOT to tell that after i press save my entire app is terminated. Here is my Code

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TakePicButton = (Button) findViewById(R.id.TakePicture);
    TakePicButton.setOnClickListener((android.view.View.OnClickListener) this);

}

@Override
public void onDestroy(){
    super.onDestroy();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();

        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);

        } else {
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);

        }
    }

 public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.TakePicture){

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
  • 1
    `onActvityResult()` should be automatically called after you press the save-button. It's hard to tell what's wrong here without some code, so please post a code snippet that shows how you call the camera activity via an intent and your full `onActivityResult()` method. Thanks. –  Dec 27 '11 at 13:49
  • Hey I put my code. If you think of something tell me. Thanks – George Melidis Dec 27 '11 at 14:31

1 Answers1

0

try the below code, you will have to modify it a bit, it will help you get From Library and From Camera both, the SELECT_PICTURE is used for getting image from library

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case SELECT_PICTURE:
            Uri selectedImageUri = data.getData();
            filemanagerstring = selectedImageUri.getPath();
            selectedImagePath = getPath(selectedImageUri);
            if (selectedImagePath != null)
                myFile = new File(selectedImagePath);
            else if (filemanagerstring != null)
                myFile = new File(filemanagerstring);
            if (myFile != null) {
                Bitmap bmp_fromGallery = decodeImageFile(selectedImagePath);


        break;
    case CAMERA_REQUEST:

            Bitmap bmp_Camera = (Bitmap) data.getExtras().get("data");

        break;
    default:
        break;
    }
}
Maulik J
  • 2,745
  • 19
  • 22
  • I tried your code but SELECT_PICTURE OR CAMERA_REQUEST are errors in code. Why it don't recognize? Thanks – George Melidis Dec 27 '11 at 14:29
  • add private static final int SELECT_PICTURE = 1; private static final int CAMERA_REQUEST = 2; – Maulik J Dec 27 '11 at 14:30
  • did u add intent --> Intent cameraIntent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST); – Maulik J Dec 27 '11 at 15:18
  • the problem is that after the button save pressed the app is terminated,the method onresultActivity is not run and i don't get back to my mainScreen. – George Melidis Dec 27 '11 at 15:39
  • Where does it crash? Please include stack trace from LogCat. – Code Poet Dec 27 '11 at 16:45