HYFY Movies
HYFY Movies

Reputation: 21

startActivityForResult doesn't work in a Fragment while image upload from camera or gallery

public class Profile extends Fragment implements Profile_frg{


imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                final Dialog d = new Dialog(mainActivity);
                d.requestWindowFeature(Window.FEATURE_NO_TITLE);
                d.setContentView(R.layout.activity_custom_dialog);
                d.setCanceledOnTouchOutside(true);


                gallery = (ImageView) d.findViewById(R.id.imageView1);
                camera = (ImageView) d.findViewById(R.id.imageView2);
                cancel = (ImageView) d.findViewById(R.id.imageView3);

                cancel.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        d.dismiss();
                    }
                });


                gallery.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        try {
                            Intent gintent = new Intent();
                            gintent.setType("image/*");
                            gintent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(
                                    gintent, "Select Picture"), PICK_IMAGE);
                        } catch (Exception e) {
                            Toast.makeText(mainActivity,
                                    e.getMessage(), Toast.LENGTH_LONG).show();
                            Log.e(e.getClass().getName(), e.getMessage(), e);
                        }

                        d.dismiss();
                    }

                });

                camera.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        // define the file-name to save photo taken by Camera
                        // activity
                        String fileName = "new-photo-name.jpg";
                        // create parameters for Intent with filename
                        ContentValues values = new ContentValues();
                        values.put(MediaStore.Images.Media.TITLE, fileName);
                        values.put(MediaStore.Images.Media.DESCRIPTION,
                                "Image captured by camera");
                        // imageUri is the current activity attribute, define
                        // and save it for later usage (also in
                        // onSaveInstanceState)
                        imageUri = context.getContentResolver().insert(
                                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                values);
                        // create new Intent
                        Intent intent = new Intent(
                                MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

                        startActivityForResult(intent, PICK_Camera_IMAGE);

                        d.dismiss();
                    }

                });
                d.show();
            }
        });

}// Work Fine till here...



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

    }//didn't detect this method

Upvotes: 0

Views: 872

Answers (2)

Ambar Jain
Ambar Jain

Reputation: 517

Yes, there is no onActivityResult() callback in fragments. You have to override activityResult method in your host activity(in which your fragment is defined)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if(requestCode == GALLERY/CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
       Fragment yourFragment = getSupportFragmentManager().findFragmentByTag("FRAGMENT_TAG"); // same tag while adding fragment for the first time.
       if (yourFragment != null) {
           yourFragment.onActivityResult(requestCode, resultCode, data); //calling method that should be defined in your fragment.
       }
   }
   super.onActivityResult(requestCode, resultCode, data);
}

And in your fragment do like this :

public void onActivityResult(int requestCode,int resultCode,Intent data) {
   ...
   Pull your image data from data object
   do your further process from here.
   ...
}

Upvotes: 2

Yasir Tahir
Yasir Tahir

Reputation: 800

Yes, startActivityForResult will not work directly if you are calling it from any fragment. After getting the result, the callback will hit the onActivityResult of the Hosting Activity from where you have to manually redirect it to the respective fragment.

Below is the sample code of the onActivityResult of your Activity. Please note that this only redirect the result to the respective fragment.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 1001:
            Fragment frag = getSupportFragmentManager().findFragmentByTag("TAG"); // TAG should be same as the one you entered while adding the fragment
            if (frag != null) {
                frag .onActivityResult(requestCode, resultCode, data);
            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

Upvotes: 0

Related Questions