27

I have an issue with importing a picture from the Album in Android, because the onActivityResult() method is never called.

This is the code that I wrote (called from a fragment not an activity):

Intent galleryIntent = new Intent(Intent.ACTION_PICK,  android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
getActivity().startActivityForResult(galleryIntent, PICK_IMAGE);

And by the way, I have defined the onActivityResult() but it's never triggered:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult"); // not printed
}

Any idea what's wrong with this?

Thanks!

4
  • 3
    add constructor , super.onActivityResult(requestCode, resultCode, data); Commented Jan 12, 2012 at 17:30
  • I have edited the question, indeed my code has already that line. Commented Jan 12, 2012 at 17:39
  • You need to Select image from Gallery right? Commented Jan 12, 2012 at 17:44
  • Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); intent.putExtra("return-data", true); startActivityForResult(intent, 1); Commented Jan 12, 2012 at 17:45

3 Answers 3

112

To have onActivityResult() called in the fragment, you should call the fragment's version of startActivityForResult(), not the activity's. So in your fragment's code, replace

getActivity().startActivityForResult(galleryIntent, PICK_IMAGE);

with

startActivityForResult(galleryIntent, PICK_IMAGE);
Sign up to request clarification or add additional context in comments.

4 Comments

So easy to make that mistake when converting Activities to Fragments :-)
WOW worked :) nice, we always use getActivity() in fragments :) help me a lot
this should be the correct answer, the other method of calling a method inside your fragment is a workaround
I lost some time trying to figure it out. Thanks for the answer! :)
16

With this code:

Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
getActivity().startActivityForResult(galleryIntent, PICK_IMAGE);

The onActivityResult must be in the Activity that contains the Fragment. From there you can call any method of the fragment, not in the fragment.

MyFragment myFragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
myFragment .onCameraResult(requestCode, resultCode, intent);

to do there whatever you want

1 Comment

Can't believe I didn't see it before! Thanks :)
-2

Try this Snippet :

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
    intent.setType("image/*");
    intent.putExtra("return-data", true);
    startActivityForResult(intent, 1);


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

        case 1:
            if(requestCode == 1 && data != null && data.getData() != null){
                Uri _uri = data.getData();

                if (_uri != null) {
                    Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
                    cursor.moveToFirst();
                    final String imageFilePath = cursor.getString(0);
                    File photos= new File(imageFilePath);
                    imageView.setImageBitmap(bitmap);
                    cursor.close();
                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }   

1 Comment

why u call "super.onActivityResult(requestCode, resultCode, data);" twice ??!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.