Android programming for getting pictures and video thumbnails _android

Source: Internet
Author: User

This article describes the Android programming approach to getting pictures and video thumbnails. Share to everyone for your reference, specific as follows:

Starting with the Android 2.2 system, a thumbnail thumbnailutils class is added, located in the Android.media.ThumbnailUtils position of the framework, Can help us get a thumbnail of the video or picture files in the system from Mediaprovider, which provides three static methods that can be invoked directly.

1. Createvideothumbnail

Static Bitmap Createvideothumbnail (String filePath, int kind)
//Get 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 related to the final and resolution of Mini_kind or Micro_kind

2. Extractthumbnail

Static Bitmap Extractthumbnail (Bitmap source, int width, int height, int options)
//Direct thumbnail operation to Bitmap
/ The last parameter is defined as Options_recycle_input to reclaim the resource

3. Extractthumbnail

Static Bitmap Extractthumbnail (Bitmap source, int width, int height)
//This is the same as the above method, no options option

Get video thumbnails from 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;
}

Get 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;
   /** * get picture and video thumbnail * These two methods must be used in version 2.2 and above, because the thumbnailutils is used in this class/public class Androidtestactivity extends activity {
   Private ImageView Imagethumbnail;
   Private ImageView Videothumbnail; /** called the activity is a.
   * * @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.getexternalstoragedirectory (). 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, MediaStore.Images.Thumbnails.MICRO_KIND)); /** * Gets the thumbnail based on the specified image path and size * This method has two points of benefit: * 1.
   Using a smaller memory space, the first bitmap is actually null, just to read the width and height, * The second read bitmap is based on the proportional compression of the image, the third read bitmap is the desired thumbnail. * 2.
   The thumbnail is not stretched for the original image, using the 2.2 version of the new tool thumbnailutils so that the image generated by the tool is not 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 Bit
   Map Getimagethumbnail (String imagepath, int width, int height) {Bitmap Bitmap = null;
   Bitmapfactory.options Options = new Bitmapfactory.options ();
   Options.injustdecodebounds = true;
   Get the width and height of this picture, note that the bitmap here is null bitmap = Bitmapfactory.decodefile (ImagePath, Options); Options.injustdecodebounds = FALse
   Set to FALSE//compute scaling 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 (is <= 0) {be = 1;
   } options.insamplesize = be; Reread the picture, read the scaled bitmap, and note that this time you want to set the Options.injustdecodebounds to False bitmap = Bitmapfactory.decodefile (ImagePath, Options
   ); Use Thumbnailutils to create thumbnails, where you specify which bitmap object to scale bitmap = Thumbnailutils.extractthumbnail (bitmap, width, height, thumbn
   Ailutils.options_recycle_input);
   return bitmap;
   /** * Get the thumbnail of the video * Create a thumbnail of the video by Thumbnailutils first, and then use thumbnailutils to generate a thumbnail of the specified size.
   * If the desired thumbnail width and height are less than micro_kind, the type uses Micro_kind as the KIND value, which saves memory. * @param videopath Video Path * @param width Specifies the width of the output video thumbnail * @param height Specifies the height of the output video thumbnail * @param kind reference Mediastore.ima Ges.
   Constants in the Thumbnails class Mini_kind and Micro_kind. * of which, mini_kind:512 x 384,micro_kind:96 x 96 * @Return the video thumbnail of the specified size/private Bitmap getvideothumbnail (String videopath, int width, int height, int kind) {Bi
   TMap bitmap = null;
   Gets the thumbnail image of the video 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;

 }
}

Layout:

<?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:o" rientation= "vertical" >
    <textview
      android:layout_width= "Fill_parent"
      Wrap_content "
      android:text=" picture 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>

For more information on Android-related content readers can view the site topics: "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View summary" and " A summary of the usage of Android controls

I hope this article will help you with the Android program.

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.