Android gets thumbnails of images and videos on sdcard

Source: Internet
Author: User

A thumbnail thumbnailutils class has been added to the system since Android 2.2, which is located in the framework's Android. media. the location of thumbnailutils can help us obtain thumbnails of video or image files in the system from mediaprovider. This class provides three static methods that can be called directly.

1.

Static bitmap createvideothumbnail (string filepath, int kind) // obtain the thumbnail of the video file. The first parameter is the location of the video file, such as/sdcard/android123.3gp, the second parameter can be mini_kind or micro_kind, which is ultimately related to resolution.

2.

Static bitmap extractthumbnail (bitmap source, int width, int height, int options) // scale bitmap directly. The last parameter is defined as options_recycle_input to recycle resources.

3.

Static bitmap extractthumbnail (bitmap source, int width, int height) // This is the same as the preceding method and has no options

========================================================== ========================================================== ====

Get video thumbnails on your phone:

public static Bitmap getVideoThumbnail(ContentResolver cr, Uri uri) {          Bitmap bitmap = null;          BitmapFactory.Options options = new BitmapFactory.Options();          options.inDither = false;          options.inPreferredConfig = Bitmap.Config.ARGB_8888;          Cursor cursor = cr.query(uri,new String[] { MediaStore.Video.Media._ID }, null, null, null);                 if (cursor == null || cursor.getCount() == 0) {              return null;          }          cursor.moveToFirst();          String videoId = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media._ID));  //image id in image table.s            if (videoId == null) {          return null;          }          cursor.close();          long videoIdLong = Long.parseLong(videoId);          bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, videoIdLong,Images.Thumbnails.MICRO_KIND, options);            return bitmap;          }  

  

========================================================== ========================================================== ============

Obtain the video thumbnails in the specified directory sdcard:

Import Java. io. file; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. media. thumbnailutils; import android. OS. bundle; import android. OS. environment; import android. provider. mediastore; import android. widget. imageview;/*** obtain image and video thumbnails * must be used in version 2.2 or later, because the thumbnailutils class */public class androidtestactivity extends activity {private Imageview imagethumbnail; private imageview videothumbnail;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); imagethumbnail = (imageview) findviewbyid (R. id. image_thumbnail); videothumbnail = (imageview) findviewbyid (R. id. video_thumbnail); string ImagePath = environment. ge Texternalstoragedirectory (). getabsolutepath () + file. separator + "photo" + file. separator + "yexuan.jpg"; string videopath = environment. getexternalstoragedirectory (). getabsolutepath () + file. separator + "video" + file. separator + "vinegar lamp. avi "; imagethumbnail. setimagebitmap (getimagethumbnail (ImagePath, 60, 60); videothumbnail. setimagebitmap (getvideothumbnail (videopath, 60, 60, mediastore. images. th Umbnails. micro_kind);}/*** obtain the thumbnail Based on the specified image path and size. * This method has two advantages: * 1. using a small memory space, the bitmap obtained for the first time is actually null, just to read the width and height. * The bitmap read for the second time is the image compressed proportionally, the third bitmap read is the desired thumbnail. * 2. The thumbnail is not stretched for the original image. Here we use the new thumbnailutils tool of Version 2.2, so that * images generated using this tool will not be stretched. * @ Param ImagePath image path * @ Param width specifies the width of the output image * @ Param height specifies the height of the output image * @ return generated thumbnail */private bitmap getimagethumbnail (string ImagePath, int width, int height) {Bitmap bitmap = NULL; bitmapfactory. options = new bitmapfactory. options (); options. injustdecodebounds = true; // obtain the width and height of the image. Note that bitmap here is null bitmap = bitmapfactory. decodefile (ImagePath, options); options. injustdecodeboun DS = false; // set to false // calculate the zoom ratio int H = options. outheight; int W = options. outwidth; int bewidth = W/width; int beheight = H/height; int be = 1; if (bewidth <beheight) {Be = bewidth;} else {Be = beheight ;} if (be <= 0) {Be = 1;} options. insamplesize = be; // read the image again and read the scaled bitmap. injustdecodebounds is set to false bitmap = bitmapfactory. decodefile (ImagePath, options); // use thumbna Ilutils to create a thumbnail. here you need to specify which bitmap object to scale bitmap = thumbnailutils. extractthumbnail (bitmap, width, height, thumbnailutils. options_recycle_input); Return bitmap;}/*** get the video thumbnail * first create a thumbnail of the video through thumbnailutils, and then use thumbnailutils to generate a thumbnail of the specified size. * If the width and height of the desired thumbnail are smaller than that of micro_kind, use micro_kind as the kind value, which saves memory. * @ Param videopath the video path * @ Param width specifies the width of the output video thumbnail * @ Param height specifies the height of the output video thumbnail * @ Param kind refer to mediastore. images. the constants mini_kind and micro_kind In the thumbnails class. * Mini_kind: 512x384, micro_kind: 96X96 * @ return specifies the video thumbnail size */private bitmap getvideothumbnail (string videopath, int width, int height, int kind) {Bitmap bitmap = NULL; // obtain the video thumbnail bitmap = thumbnailutils. createvideothumbnail (videopath, kind); system. out. println ("W" + bitmap. getwidth (); system. out. println ("H" + bitmap. getheight (); bitmap = thumbnailutils. extractthumbnail (bitmap, width, height, thumbnailutils. options_recycle_input); Return bitmap ;}}

  

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "image thumbnail"/> <imageview Android: Id = "@ + ID/image_thumbnail" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: TEXT = "video thumbnails"/> <imageview Android: Id = "@ + ID/video_thumbnail" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> </linearlayout>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.