Android Gallery always returns RESULT_CANCELED to onActivityResult

Go To StackoverFlow.com

1

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?

2012-04-03 22:00
by Velo Steve
Seems like it's a bug in Kindle Fire. Perhaps a duplicate of http://stackoverflow.com/questions/9951006/android-image-picker-doesnt-work-on-kindle-fire/9951054#995105 - Jon O 2012-04-03 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 2012-04-04 16:55


1

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);
2012-04-19 08:35
by Joey


1

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.

2018-01-01 18:33
by Ankit Gaur


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);  
2012-04-03 22:05
by Martin Sykes
Thanks, but this gives exactly the same result as my code - Velo Steve 2012-04-04 16:50
Ads