Skip to content Skip to sidebar Skip to footer

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 Ur

Solution 1:

You can try with this:

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

    Cursor ca = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, newString[] { MediaStore.MediaColumns._ID }, MediaStore.MediaColumns.DATA + "=?", newString[] {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();
    returnnull;

}

Solution 2:

You can get the Uri from a file like this:

Uriuri= Uri.fromFile(newFile("/mnt/images/abc.jpg"));
Bitmapthumbnail= getPreview(uri);

And the following function gives you the thumbnail:

Bitmap getPreview(Uri uri) {
    Fileimage=newFile(uri.getPath());

    BitmapFactory.Optionsbounds=newBitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image.getPath(), bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1))
        returnnull;

    intoriginalSize= (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
            : bounds.outWidth;

    BitmapFactory.Optionsopts=newBitmapFactory.Options();
    opts.inSampleSize = originalSize / THUMBNAIL_SIZE;
    return BitmapFactory.decodeFile(image.getPath(), opts);     
}

Solution 3:

It could be a alternative ways as other had already mentioned in their answer but Easy way i found to get thumbnail is using ExifInterface

ExifInterfaceexif=newExifInterface(pictureFile.getPath());
    byte[] imageData=exif.getThumbnail();
    if (imageData!=null) //it can not able to get the thumbnail for very small images , so better to check null
    Bitmap  thumbnail= BitmapFactory.decodeByteArray(imageData,0,imageData.length);

Solution 4:

You can simply create thumbnail video and image using ThumnailUtil class of java

Bitmapresized= ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height);


 publicstatic Bitmap createVideoThumbnail(String filePath, int kind)

Added in API level 8 Create a video thumbnail for a video. May return null if the video is corrupt or the format is not supported.

Parameters filePath the path of video file kind could be MINI_KIND or MICRO_KIND

For more Source code of Thumbnail Util class

Developer.android.com

Solution 5:

Use android Bitmap class and its createScaledBitmap method.

method descroption:

publicstatic Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)

example of using:

Bitmapbitmap= BitmapFactory.decodeFile(img_path);
intorigWidth= bitmap.getWidth();
intorigHeight= bitmap.getHeight();
bitmap = Bitmap.createScaledBitmap(bitmap, origWidth / 10, origHeight / 10, false);

decrease the source file like you want. In my example I reduced it 10 times.

Post a Comment for "Android Get Thumbnail Of Image Stored On Sdcard Whose Path Is Known"