2

First of all, this is NOT the frequently-posted problem where the result code is returned prematurely. In this case, it is returned only after an item is picked in the gallery.

In my test case I call the Gallery with this code:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,  SELECT_PICTURE);

and in onActivityResult there is:

if (resultCode == Activity.RESULT_OK) {
    if (requestCode == SELECT_PICTURE) {
        Uri selectedImageUri = data.getData();
        pathText.setText(selectedImageUri.getPath());
    }
} else {
    pathText.setText("Result not OK - code is " + resultCode);
}

pathText is just a TextView I put in to show the result in the test case. In the actual application there is a different use for the path.

If I use ACTION_PICK instead of _GET_CONTENT I get the immediate failure reported by others. There are no launchMode tags in the manifest (some posts have suggested problems in that area).

Maybe there is a clue here. On a Toshiba Thrive, this bug does not appear, using Gallery, File Manager, or Fish Bowl Photo Gallery. On Kindle Fire, Quickoffice is also able to return an image path correctly. The bug only appears for me on the Kindle's built-in Gallery. The bug was also observed on a "Motorola Droid(2.3.4) , HTC EVO (2.3)".

Please, how can I get an image path back from the Gallery in a way that works on all these devices?

2
  • Seems like it's a bug in Kindle Fire. Perhaps a duplicate of stackoverflow.com/questions/9951006/…
    – Jon O
    Apr 3, 2012 at 22:05
  • I think you must be right. I'm sure I tested this code when I wrote it weeks ago, but it failed recently.
    – Velo Steve
    Apr 4, 2012 at 16:55

4 Answers 4

7

I was having same problem in one of my activities when I set launchMode="singleInstance" in manifest for that activity. It works fine when I remove that attribute.

1
  • 3
    fun fact: if you use startActivityForResult(Intent.createChooser(intent, null), code) it works even with lauchMode="singleInstance" - thank you for this hint!
    – Christian
    Jun 7, 2021 at 11:44
2

I think, by default gallery does not return result code if you not specify in the intent to return result code. You can specify in the intent to return result code by adding this snippet in your code like this:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true); //added snippet
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_PICTURE);
0

Hi Steve try this seems to work on my wallpaper project

Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_PICTURE);  
0
0

If using registerForActivityResult you can simply do this:

val galleryLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){result->
   if(result.resultCode == Activity.RESULT_OK){
    // Handle result here
   }
}
val galleryIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
galleryLauncher.launch(Intent.createChooser(galleryIntent, null)

If passing galleryIntent directly without the Intent.createChooser I was facing the RESULT_CANCELED issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.