6

I'm starting an intent to pick a picture from the gallery but the intent always returns with the resultcode RESULT_CANCELED. I have tried a lot of different code but nothing helps which makes me think maybe I am missing something, like putting something in the activity in the Android manifest?

My Code:

// The Intent
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK){
        Uri targetUri = data.getData();
        Bitmap bitmap;
        try {
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
            profileImage.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
DecodeGnome
  • 1,809
  • 1
  • 19
  • 36
  • put your activity finish() code also – Sunil Pandey Mar 14 '11 at 10:32
  • Hmm do you mean putting finish() after startActivityForResult? The only thing this does for me is finishing the activity (wich I don't want, I want it to return to the same activity) and still with RESULT_CANCELED. – DecodeGnome Mar 14 '11 at 10:41

2 Answers2

20

OK so I solved this. My problem turned out to be that the onActivityResult() method was being called before the Gallery Intent had finished. I found the sollution here: onActivityResult() called prematurely

Basically, I had specified the activity to be "singleTask" in the manifest. Changing it to "singleTop" solved it for me.

Community
  • 1
  • 1
DecodeGnome
  • 1,809
  • 1
  • 19
  • 36
1

That saved my life! \0/

android:launchMode="singleTop"

Sena
  • 11
  • 1