This article describes the method of Android programming to obtain pictures and video thumbnails. Share it for your reference, as follows:
Starting from Android 2.2, a thumbnail Utils class has been added, located in the framework. It can help us obtain thumbnails of video or image files in the system from mediaprovider. This class provides three static methods that can be directly called to obtain.
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 MINI_KIND or MICRO_KIND. Finally, it is related to resolution.
2. extractThumbnail
static Bitmap extractThumbnail(Bitmap source, int width, int height, int options) //Directly perform thumbnail operation on Bitmap//The last parameter is defined as OPTIONS_RECYCLE_INPUT to recycle resources
3. extractThumbnail
static Bitmap extractThumbnail(Bitmap source, int width, int height) // This is the same as the above method, without options
Get thumbnails of videos on your phone:
public static Bitmap getVideoThumbnail(ContentResolver cr, Uri uri) { Bitmap bitmap = null; options = new (); = false; = .ARGB_8888; Cursor cursor = (uri,new String[] { ._ID }, null, null, null); if (cursor == null || () == 0) { return null; } (); String videoId = ((._ID)); //image id in image if (videoId == null) { return null; } (); long videoIdLong = (videoId); bitmap = (cr, videoIdLong,.MICRO_KIND, options); return bitmap; }
Get the video thumbnail in the specified directory sdcard:
import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Get thumbnails for pictures and videos * These two methods must be used in version 2.2 and above, because the ThumbnailUtils class is used. */ public class AndroidTestActivity extends Activity { private ImageView imageThumbnail; private ImageView videoThumbnail; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); imageThumbnail = (ImageView) findViewById(.image_thumbnail); videoThumbnail = (ImageView) findViewById(.video_thumbnail); String imagePath = () .getAbsolutePath() + + "photo" + + ""; String videoPath = () .getAbsolutePath() + + "video" + + "Vinegar lights.avi"; (getImageThumbnail(imagePath, 60, 60)); (getVideoThumbnail(videoPath, 60, 60, .MICRO_KIND)); } /** * Get thumbnails based on the specified image path and size * This method has two benefits: * 1. Using smaller 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 an image compressed according to the proportion, and the bitmap read for the third time is the desired thumbnail. * 2. The thumbnail image is not stretched for the original image. Here we use the new tool ThumbnailUtils, version 2.2, to make * Images generated with 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; options = new (); = true; // Get the width and height of this picture, note that the bitmap here is null bitmap = (imagePath, options); = false; // Set to false // Calculate the scaling ratio int h = ; int w = ; int beWidth = w / width; int beHeight = h / height; int be = 1; if (beWidth < beHeight) { be = beWidth; } else { be = beHeight; } if (be <= 0) { be = 1; } = be; //Read the picture again and read the zoomed bitmap. Note that this time you should set it to false bitmap = (imagePath, options); // Use ThumbnailUtils to create thumbnails, here you need to specify which Bitmap object to zoom in bitmap = (bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); return bitmap; } /** * Get a thumbnail of the video * First, use ThumbnailUtils to create a thumbnail of a video, and then use ThumbnailUtils to generate a thumbnail of a specified size. * If the width and height of the desired thumbnail are smaller than MICRO_KIND, then the type should use MICRO_KIND as the value of kind, which will save 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 Refer to the constants MINI_KIND and MICRO_KIND in the class. * Where, MINI_KIND: 512 x 384, MICRO_KIND: 96 x 96 * @return Video thumbnail of specified size */ private Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind) { Bitmap bitmap = null; // Get a thumbnail of the video bitmap = (videoPath, kind); ("w"+()); ("h"+()); bitmap = (bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); return bitmap; } }
layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro 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="Picture Thumbnail" /> <ImageView android: 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" /> <ImageView android: android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
For more information about Android related content, please check out the topic of this site:Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.