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.
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: