-1

I have a fragment where I am calling getActivity().startActivityForResult for camera activity and I have onActivityResult in my MainActivity to handle the Result.

Fragment

Intent intent = new Intent();

intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
try {
    intent.putExtra("return-data", true);
    getActivity().startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
   // Do nothing for now
}

MainActivity

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);


if (resultCode == RESULT_CANCELED) //CANCELED
{
    Toast.makeText(this, "canceled", Toast.LENGTH_SHORT).show();
}

switch (requestCode) {
    case PICK_FROM_GALLERY:


        Toast.makeText(this, "Pick from Gallery", Toast.LENGTH_SHORT).show();
            if (resultCode == RESULT_OK) {

                Toast.makeText(this, "Result Okay", Toast.LENGTH_SHORT).show();
                Bundle extras2 = data.getExtras();
                if (extras2 != null) {
                    //Doesn't enter here
                } else {
                    Toast.makeText(this, "extra is null", Toast.LENGTH_SHORT).show();
                }
            }
            break;

    }
}
dharanbro
  • 1,327
  • 4
  • 17
  • 40

3 Answers3

1

Activity @Oncreate open camera intent

// Camera Option Clicked    
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, 1);   

Handle onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case 1:

        if(resultCode == RESULT_OK){

            if (data != null) {

                takePhoto(data);
            }
        }

        break;
    }

}

Display image on ImageView

private void takePhoto(Intent imageData){
    Bundle extras = imageData.getExtras();

    if(extras != null){
        imageView.setImageBitmap((Bitmap) extras.get("data"));
    }
}
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39
0

change this line

getActivity().startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);

to

startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);

refer this for further information onActivityResult is not being called in Fragment

Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_PICK);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
                        112);
if (requestCode == 112) {
        try {
            InputStream inputStream = getContentResolver()
                    .openInputStream(data.getData());
            FileOutputStream fileOutputStream = new FileOutputStream(
                    mFileTemp);
            copyStream(inputStream, fileOutputStream);// do other stuff
            fileOutputStream.close();
            inputStream.close();
            //do other stuff
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Community
  • 1
  • 1
Sridhar
  • 668
  • 1
  • 11
  • 22
-1

You should call startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY); from your fragment and then implement the onActivityResult() in your Fragment itself, and in the onActivityResult() just check for the result code as

public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == PICK_FROM_GALLERY) {

Vaibhav Barad
  • 625
  • 8
  • 17
  • still the `data.getExtras()` is null – dharanbro Sep 09 '15 at 07:05
  • how are you checking if the data.getExtras().? i do this if (requestCode == PICK_FROM_GALLERY) {if (resultCode == Activity.RESULT_OK) { Bundle extras = data.getExtras();if (extras != null) {BitmapDrawable cropped_image = extras.getParcelable("data"); – Vaibhav Barad Sep 09 '15 at 07:28
  • did you read the question. All these were already done. I am getting null in `extras` – dharanbro Sep 09 '15 at 07:43