Android Get Thumbnail Of Image Stored On Sdcard Whose Path Is Known
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
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"