I am new at this 🙂
I am trying to load a picture from the sdcard into an ImageView in this way:
b_picture.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
});
Here I am trying to retrive the picture and change the ImageView:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bm = (Bitmap) data.getExtras().get("data");
iv_picture.setImageBitmap(bm);
}
And I get this from the logcat:
Failure delivering result ResultInfo {who=null, request=0, result=-1,
data=Intent { dat=content://media/external/images/media/2
I can’t solve this problem. Can you help me? Thanks.
The data associated with your returned Intent is not a bitmap. It’s a URI you can use to look up in the MediaStore ContentProvider to get back the image you want. 🙂
You can find a mostly-working example over at this question.
Edit: To expand:
When you go off and query the
MediaStorefor an image, it isn’t returning you the actual image. It’s returning you a URI that you can use to look up the image. The way you translate that URI into an actual image is this:Your URI is
content://media/external/images/media/2as per the error message.So, we’ll basically create a query and run it on the
MediaStore‘sContentProvider, which is a database of images. Pass that URI into this function: