0

I have two activities: activity_main and activity_takeapicture. I start an intent from activity_main and the other activity takes a picture. Then activity_takeapicture sends another intent with the file back to activity_main. In activity_main should I use onActivityResult method to catch the intent sent by activity_takeapicture and subsequently receive the data? Here is the part that sends the intent in activity_main:

    Intent intent = new Intent(this, DisplayResult.class);
    startActivity(intent);

And here is 'activity_takeapicture`:

Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra(Intent.EXTRA_STREAM, pictureFile);
        startActivity(intent);

EDIT: I have tried what you guys said, but now onActivityResult doesn't fire when the activity_takeapicture returns the intent back. How do I fix that?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
SalmonKiller
  • 2,183
  • 4
  • 27
  • 56

4 Answers4

0

you can do it like that but you will have to close all of the intent that you left open to avoid memory uses and when you back press the you wont see over 9,000 activities xD it wont avoid memory uses because it will close te Activity only after you return to it.

you can also use the alnActivityResult, this is better solution because you are not opening too much Activities and it is simple to you and to your user

0

If activity_takeapicture saves the picture on the device SD card, you can use the following.

intent.putExtra("SavedImageDirectory", filepath); //this will save the file path 

then in your activity_main you can use

//this will get the file path of the saved image
String ImageDirectory = getIntent().getStringExtra("SavedImageDirectory"); 

//this will decode the filepath into a bitmap 
Bitmap bp = BitmapFactory.decodeFile(ImageDirectory);

//this will display the bitmap on your imageview on the `activity_main`
MyImageView.setImageDrawable(bp);
tashad
  • 258
  • 2
  • 9
0

Try this:

In your Class inialize a static Variable

private static final int CAM_REQUEST = 1;

In your Button Listener Method

public void onClick(View v){
   Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
   startActivityForResult(takePictureIntent, CAM_REQUEST);
}

And then in Your Activity Override this method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
   if(requestCode == CAM_REQUEST){
      Bitmap thumbTaken = (Bitmap) data.getExtras().get("data");
      yourImageView.setImageBitmap(thumbTaken);
   }
}

And dont forget to give permission for using CAMERA

Hope it helps!!!

Kostas Drak
  • 3,222
  • 6
  • 28
  • 60
0

Do not use startActivity. Use startActivityForResult instead.

Philip Sheard
  • 5,789
  • 5
  • 27
  • 42