Android gets thumbnails of pictures and videos on SDcard

Source: Internet
Author: User

Ways to get thumbnail images and video thumbnails:

Java code:

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;/** * Get thumbnails of images and videos * These two methods must be used in versions 2.2 and above, as they use the Thumbnailutils class*/ Public classAndroidtestactivity extends Activity {PrivateImageView Imagethumbnail; PrivateImageView Videothumbnail; /** Called when the activity is first created.*/@Override Public voidonCreate (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.getexternalstoragedirectory (). GetAbsolutePath ()+File.separator+"Photo"+File.separator+"yexuan.jpg"; String Videopath=environment.getexternalstoragedirectory (). GetAbsolutePath ()+File.separator+"Video"+File.separator+"vinegar Lighting. avi"; Imagethumbnail.setimagebitmap (Getimagethumbnail (ImagePath, -, -)); Videothumbnail.setimagebitmap (Getvideothumbnail (Videopath, -, -, MediaStore.Images.Thumbnails.MICRO_KIND)); }    /** * Get thumbnails based on specified image path and size * This method has two benefits: * 1. Using a smaller memory space, the first fetch of the bitmap is actually null, just to read the width and height, * second Read     The bitmap is based on the proportional compressed image, the third read of the bitmap is the desired thumbnail image. * 2.     Thumbnails are not stretched for the original image, and the 2.2 version of the new tool thumbnailutils is used, so that the images generated with this tool are not stretched. * @param the path of the ImagePath image * @param width specifies the output image widths * @param height Specifies the height of the output image * @return generated thumbnails*/    PrivateBitmap Getimagethumbnail (String ImagePath,intWidthintheight) {Bitmap Bitmap=NULL; Bitmapfactory.options Options=Newbitmapfactory.options (); Options.injustdecodebounds=true; //get the width and height of this picture, note that the bitmap here is nullBitmap =bitmapfactory.decodefile (ImagePath, Options); Options.injustdecodebounds=false;//set to False//Calculating the zoom ratio        inth =Options.outheight; intW =Options.outwidth; intBewidth = w/width; intBeheight = h/height; intbe =1; if(Bewidth <beheight) { be=Bewidth; } Else{ be=Beheight; }        if(Be <=0) { be=1; } options.insamplesize=Be ; //re-read the picture, read the scaled bitmap, and note this time to set the Options.injustdecodebounds to FalseBitmap =bitmapfactory.decodefile (ImagePath, Options); //use Thumbnailutils to create thumbnails, where you want to specify which bitmap object to scaleBitmap =thumbnailutils.extractthumbnail (bitmap, width, height, thumbnailutils.options_recycle_input); returnbitmap; }    /** * Get thumbnail image of video * First create a thumbnail of a video with Thumbnailutils, and then use thumbnailutils to generate a thumbnail of the specified size.     * If the desired thumbnail width and height are less than micro_kind, then the type will use Micro_kind as the KIND value, which saves memory. * @param the path of the Videopath video * @param width Specifies the width of the output video thumbnail * @param height Specifies the height of the output video thumbnail * @param kind reference mediastore.     Constants Mini_kind and Micro_kind in the Images.thumbnails class. * Where mini_kind:512 x 384,micro_kind:96 x * * @return A video thumbnail of the specified size*/    PrivateBitmap Getvideothumbnail (String Videopath,intWidthintheight,intkind) {Bitmap Bitmap=NULL; //get thumbnail images of a videoBitmap =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); returnbitmap; }    }


Main.xml file:

<?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="thumbnail Image"/> <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 thumbnail image"/> <ImageView Android:id="@+id/video_thumbnail"Android:layout_width="wrap_content"Android:layout_height="wrap_content"/></linearlayout>

Android gets thumbnail images of sdcard tablets and videos

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.