Android system details: Getting thumbnails of images and videos

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 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 method is the same as the preceding method. Without the options option, you can obtain the video thumbnails on your phone :[ Java] 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); 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;} get the video thumbnail in the specified directory sdcard: [java] import java. io. file; I Mport 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 the thumbnails of images and videos *. These two methods must be used in version 2.2 or later, because the ThumbnailUtils class is used. */public class AndroidTestActivity extends Activity {private ImageView I MageThumbnail; 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. getExternalSt OrageDirectory (). 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. thumbnails. MI CRO_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 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, heig Ht, ThumbnailUtils. OPTIONS_RECYCLE_INPUT); return bitmap;} layout: [java] <? 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 www.2cto.com 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>

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.