I am following this sample code to send an intent to get an image.
Intent i = new Intent();
i.setAction(Intent.ACTION_GET_CONTENT);
i.setType("image/*");
if (i.resolveActivity(getPackageManager()) != null) {
startActivityForResult(i, 3);
}
In onActivityResult
:
if(requestCode == 3 && resultCode == RESULT_OK) {
Bitmap thumbnail = data.getParcelableExtra("data");
if(thumbnail == null) {
Toast.makeText(getApplicationContext(), "Thumbnail is null", Toast.LENGTH_SHORT).show();
return;
}
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.setImageBitmap(thumbnail);
}
When the intent is sent, the image picker launches, and I select one of the images in the gallery. But in the onActivityResult
, corresponding to the following statement in the sample code (I changed to getParcelableExtra
in Android 4.4):
Bitmap thumbnail = data.getParcelable("data");
I get the thumbnail as null. I am testing on Genymotion VM.
What am I doing wrong ?
Can you try his answer? How to pick an image from gallery (SD Card) for my app?
use
getData()
instead ofgetParcelableExtra()
It will return you an Uri and you will need to use the
ContentResolver
to get the Image.Also, use
Intent.ACTION_PICK
instead ofIntent.ACTION_GET_CONTENT
.Try this.. It help you
Instead of getting the bitmap, fetch the URI from data and load the bitmap yourself.