Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 編程中獲取圖片與視頻縮略圖的方法

編輯:關於 Android 程式設計

本文為大家分享 Android 中獲取圖片和視頻縮略圖的方法。

從 Android 2.2 開始,系統新增了 ThumbnailUtils 類(位於 framework 的 android.media.ThumbnailUtils),可以幫助我們從 mediaprovider 中獲取視頻或圖片文件的縮略圖。該類提供了三個靜態方法可直接調用。

1. createVideoThumbnail

 static Bitmap createVideoThumbnail(String filePath, int kind) // 獲取視頻文件的縮略圖 // 第一個參數為視頻文件的位置,比如 /sdcard/android123.3gp // 第二個參數可為 MINI_KIND 或 MICRO_KIND,最終和分辨率有關

2. extractThumbnail

 static Bitmap extractThumbnail(Bitmap source, int width, int height, int options) // 直接對 Bitmap 進行縮略操作 // 最後一個參數定義為 OPTIONS_RECYCLE_INPUT,來回收資源

3. extractThumbnail(重載)

 static Bitmap extractThumbnail(Bitmap source, int width, int height) // 和上面的方法一樣,無 options 選項

獲取手機裡視頻縮略圖

 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)); 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; }

獲得指定目錄下視頻縮略圖(含圖片縮略圖)

 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; /** 獲取圖片和視頻的縮略圖,這兩個方法必須在 2.2 及以上版本使用 */ public class AndroidTestActivity extends Activity { private ImageView imageThumbnail; private ImageView videoThumbnail; @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 + "醋點燈.avi"; imageThumbnail.setImageBitmap(getImageThumbnail(imagePath, 60, 60)); videoThumbnail.setImageBitmap(getVideoThumbnail(videoPath, 60, 60, MediaStore.Images.Thumbnails.MICRO_KIND)); } /** 根據指定的圖像路徑和大小獲取縮略圖 * 此方法有兩點好處:1. 使用較小的內存空間;2. 縮略圖對原圖像沒有拉伸。 */ private Bitmap getImageThumbnail(String imagePath, int width, int height) { Bitmap bitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 獲取圖片的寬和高,此處 bitmap 為 null bitmap = BitmapFactory.decodeFile(imagePath, options); options.inJustDecodeBounds = false; // 計算縮放比 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; // 重新讀入圖片,讀取縮放後的 bitmap bitmap = BitmapFactory.decodeFile(imagePath, options); // 利用 ThumbnailUtils 創建縮略圖 bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); return bitmap; } /** 獲取視頻的縮略圖 * kind 參照 MediaStore.Images.Thumbnails 類中的常量 MINI_KIND 和 MICRO_KIND * MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96 */ private Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind) { Bitmap bitmap = null; // 獲取視頻的縮略圖 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="https://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="圖片縮略圖" /> <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="視頻縮略圖" /> <ImageView android:id="@+id/video_thumbnail" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

其中,圖片縮略圖採用了「兩次解碼」的技巧:第一次用 inJustDecodeBounds=true 只讀取寬高而不加載像素,據此計算縮放比 inSampleSize,第二次再真正加載壓縮後的圖,最後用 ThumbnailUtils.extractThumbnail 生成不拉伸的縮略圖。希望本文對大家有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved