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
Asked
Active
Viewed 3.0k times
17

pankajagarwal
- 13,462
- 14
- 54
- 65
-
1You can simply create thumbnail video and image using ThumnailUtil class of java Bitmap resized = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height); – sujith s Apr 01 '15 at 05:37
5 Answers
24
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;
}

agirardello
- 2,895
- 22
- 22
-
1thanks you sir! and obvious, but someone might needs it, if you want to show bigger images, use MediaStore.Images.Thumbnails.MINI_KIND – keybee Jun 12 '15 at 11:31
-
Best solution that I have found. Thank You very much, this should be accepted answer. – Tunerx Jun 20 '15 at 21:34
-
1MINI_KIND is used for : 512 x 384 thumbnail MICRO_KIND is used for : 96 x 96 thumbnail – Ashish Saini Sep 15 '16 at 11:33
4
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
ExifInterface exif = new ExifInterface(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);

dharmendra
- 7,835
- 5
- 38
- 71
-
In some cases, it is not work. Not better than get from Image Store as in the accepted answer. – Huy Tower May 12 '15 at 04:54
-
4
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);
}
-
8I think using `MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), Long.parseLong(_imageUri.getLastPathSegment()), type, null);` would be a better option, since the system most of the time caches the thumbnails – pankajagarwal Dec 05 '11 at 09:53
-
2your method to get the uri doesnot seem to be working correctly because for a file with path "/mnt/sdcard/2011-12-02 11.22.50.jpg" the actual uri is ontent://media/external/images/media/556 where as the Uri.fromFile(...) method returns file:///mnt/sdcard/2011-12-02%2011.22.50.jpg – pankajagarwal Dec 05 '11 at 10:06
2
You can simply create thumbnail video and image using ThumnailUtil class of java
Bitmap resized = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height);
public static 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

sujith s
- 864
- 11
- 20
0
Use android Bitmap class and its createScaledBitmap method.
method descroption:
public static Bitmap createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter)
example of using:
Bitmap bitmap = BitmapFactory.decodeFile(img_path);
int origWidth = bitmap.getWidth();
int origHeight = 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.

Yuliia Ashomok
- 8,336
- 2
- 60
- 69