6

I want to display all photos form my phone and display them in gridview using Picasso. The problem is I don't know how to implement this.

Currentyly im using this to query all phone photos:

Cursor cursor;
    String[] columns = new String[] {
            MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.ImageColumns.TITLE,
            MediaStore.Images.ImageColumns.DATA};
    cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            columns, null, null, null);
    int columnIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails._ID);
    int columnPath = cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA);

And MediaStore.Images.Thumbnails.getThumbnail to get thumbnail's bitmap to inject to ImageView.

How could I implement it using Picasso?

Dabler
  • 877
  • 1
  • 7
  • 11

1 Answers1

4

Pretty simple. Just use the Uri.withAppendedPath to build the URI and then feed it to Picasso. The latter will internally use its MediaStoreRequestHandler to fetch the right image.

// Use the cursor you've defined (correctly)
int columnIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails._ID);
Uri imageURI = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(columnIndex));
Picasso
  .with(context)
  .load(imageURI)
  .fit()
  .centerInside()
  .into(imageView);
Sebastiano
  • 12,289
  • 6
  • 47
  • 80
  • 2
    This will not obtain the correct thumbnail, as you're providing the `columnIndex` as the ID of the image to display. Using the default projection, the column index is 0, so you will simply obtain the first thumbnail in the media store. – Paul Lammertsma Mar 02 '16 at 14:24