Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android get thumbnail of image stored on sdcard whose path is known

Say I have an image stores at path "/mnt/images/abc.jpg". How do I get the system generated thumbnail bitmap for this image. I know how to get the thumbnail of an image from its Uri, but not from its file-path

like image 933
pankajagarwal Avatar asked Dec 05 '11 09:12

pankajagarwal


2 Answers

You can try with this:

public static Bitmap getThumbnail(ContentResolver cr, String path) throws Exception {

    Cursor ca = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.MediaColumns._ID }, MediaStore.MediaColumns.DATA + "=?", new String[] {path}, null);
    if (ca != null && ca.moveToFirst()) {
        int id = ca.getInt(ca.getColumnIndex(MediaStore.MediaColumns._ID));
        ca.close();
        return MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MICRO_KIND, null );
    }

    ca.close();
    return null;

}
like image 60
agirardello Avatar answered Oct 07 '22 20:10

agirardello


You can get the Uri from a file like this:

Uri uri = Uri.fromFile(new File("/mnt/images/abc.jpg"));
Bitmap thumbnail = getPreview(uri);

And the following function gives you the thumbnail:

Bitmap getPreview(Uri uri) {
    File image = new File(uri.getPath());

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image.getPath(), bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1))
        return null;

    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
            : bounds.outWidth;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / THUMBNAIL_SIZE;
    return BitmapFactory.decodeFile(image.getPath(), opts);     
}
like image 38
Caner Avatar answered Oct 07 '22 18:10

Caner