2

Following is my code:

    if(v.getId() == R.id.button2)
    {
        Intent wpIntent = new Intent();
        wpIntent.setType("image/*");
        wpIntent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(wpIntent, "Select Picture"), SELECT_PICTURE);
    }
}

public void onActivityResult(int resultCode, int requestCode, Intent data)
{
    if(resultCode == RESULT_OK)
    {
        if(requestCode == SELECT_PICTURE)
        {
            Uri selectedImage = data.getData();
            selectedImagePath = getPath(selectedImage);
            Toast.makeText(this, ""+selectedImagePath, Toast.LENGTH_SHORT).show();
        }
    }
}

private String getPath(Uri selectedImage)
{
    String[] proj = {MediaStore.Images.Media.DATA};
    cursor = managedQuery(selectedImage, proj, null, null, null);
    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(columnIndex);
}

What's wrong with this picture? The Toast in onActivityResult doesn't get shown which means that this thing ain't working.

What am I doing wrong here?

EDIT: I added some more check and turns out the if(resultCode == RESULT_OK) is not returning true. Why is that so?

EDIT 2 This is weird. Removing both the 'if' statements makes the program work just fine. The proper image Uri is generated and the whole thing works just fine. I still don't understand why the 'if' statements return false though.

Asim
  • 6,962
  • 8
  • 38
  • 61
  • If you are not getting RESULT_OK back it means something has gone wrong in the activity, but I am not sure exactly how the ACTION_CHOOSER thing works. You basically need to find out why that is returning cancel (unless you are just pressing the back key rather than selecting something, in which case what is happening is precisely what you want!) – T. Kiley Apr 08 '12 at 18:49
  • can you check what value you are getting for resultCode and requestCode also tell me the value for SELECT_PICTURE – Shankar Agarwal Apr 09 '12 at 05:59

3 Answers3

0
Uri selectedImage = data.getData();
selectedImagePath = getPath(selectedImage);

Use the below code and try:::

Uri selectedImageuri = Uri.parse(data.getDataString());

you can do any thing with this uri(can set to imageview or convert it to bitmap).

Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • I added some more Toasts and it turns out that the if(resultCode == RESULT_OK) line is not returning true, and hence the program doesn't go forward. Reason? – Asim Apr 08 '12 at 17:00
  • Gallery opens just fine. When I click on an image, it is selected and gallery exits normally. However, the if(resultCode == RESULT_OK) returns false and hence the program doesn't go into the if statement. – Asim Apr 09 '12 at 03:38
0

result code is result_cancelled whenever there is some problem in child activity.. otherwise result_ok code is there.

See Get/pick an image from Android's built-in Gallery app programmatically to check the correct working example...

Community
  • 1
  • 1
Rahul garg
  • 9,202
  • 5
  • 34
  • 67
0

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 wpIntent = new Intent();
wpIntent.setType("image/*");
wpIntent.setAction(Intent.ACTION_GET_CONTENT);
wpIntent.putExtra("return-data", true); //added snippet
startActivityForResult(Intent.createChooser(wpIntent, "Select Picture"),SELECT_PICTURE);
Joey
  • 695
  • 6
  • 19