0

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.

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
roiberg
  • 13,629
  • 12
  • 60
  • 91

2 Answers2

2

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 MediaStore for 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/2 as per the error message.

So, we'll basically create a query and run it on the MediaStore's ContentProvider, which is a database of images. Pass that URI into this function:

public Bitmap loadFullImage( Context context, Uri photoUri  ) {
    Cursor photoCursor = null;

    try {
        // Attempt to fetch asset filename for image

        // DATA is the column name in the database for the filename of the image
        String[] projection = { MediaStore.Images.Media.DATA };

        // use the URI you were given in order to look up the right image,
        // and get a Cursor object that will iterate over the matching rows in the 
        // database.
        photoCursor = context.getContentResolver().query( photoUri, 
                                                projection, null, null, null );

        // since we only care about one image...
        if ( photoCursor != null && photoCursor.getCount() == 1 ) {

            // go to the first row that was returned
            photoCursor.moveToFirst();

            // get the string in the DATA column at that row
            String photoFilePath = photoCursor.getString(
                photoCursor.getColumnIndex(MediaStore.Images.Media.DATA) );

            // Load image from path
            return BitmapFactory.decodeFile( photoFilePath, null );
        }
    } finally {

        // close up the cursor
        if ( photoCursor != null ) {
            photoCursor.close();
        }
    }

    return null;
}
Community
  • 1
  • 1
Jon O
  • 6,532
  • 1
  • 46
  • 57
0

// path ex: /sdcard/myimage.png

String imageInSD = "Your image path here"; 
BitmapFactory.Options bOptions = new BitmapFactory.Options();
bOptions.inTempStorage = new byte[16*1024];

bitmap = BitmapFactory.decodeFile(imageInSD,bOptions);

imageView.setImageBitmap(bitmap);
MAC
  • 15,799
  • 8
  • 54
  • 95