獲取圖片縮略圖和視頻縮略圖

獲取圖片縮略圖和視頻縮略圖的方法:

  

Java代碼:

  1. import java.io.File;  
  2. import android.app.Activity;  
  3. import android.graphics.Bitmap;  
  4. import android.graphics.BitmapFactory;  
  5. import android.media.ThumbnailUtils;  
  6. import android.os.Bundle;  
  7. import android.os.Environment;  
  8. import android.provider.MediaStore;  
  9. import android.widget.ImageView;  
  10. /** 
  11.  * 獲取圖片和視頻的縮略圖 
  12.  * 這兩個方法必須在2.2及以上版本使用,因爲其中使用了ThumbnailUtils這個類 
  13.  */  
  14. public class AndroidTestActivity extends Activity {  
  15.     private ImageView imageThumbnail;  
  16.     private ImageView videoThumbnail;  
  17.   
  18.     /** Called when the activity is first created. */  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.   
  24.         imageThumbnail = (ImageView) findViewById(R.id.image_thumbnail);  
  25.         videoThumbnail = (ImageView) findViewById(R.id.video_thumbnail);  
  26.   
  27.         String imagePath = Environment.getExternalStorageDirectory()  
  28.                 .getAbsolutePath()  
  29.                 + File.separator  
  30.                 + "photo"  
  31.                 + File.separator  
  32.                 + "yexuan.jpg";  
  33.   
  34.         String videoPath = Environment.getExternalStorageDirectory()  
  35.                 .getAbsolutePath()  
  36.                 + File.separator  
  37.                 + "video"  
  38.                 + File.separator  
  39.                 + "醋點燈.avi";  
  40.           
  41.         imageThumbnail.setImageBitmap(getImageThumbnail(imagePath, 6060));  
  42.         videoThumbnail.setImageBitmap(getVideoThumbnail(videoPath, 6060,  
  43.                 MediaStore.Images.Thumbnails.MICRO_KIND));  
  44.     }  
  45.   
  46.     /** 
  47.      * 根據指定的圖像路徑和大小來獲取縮略圖 
  48.      * 此方法有兩點好處: 
  49.      *     1. 使用較小的內存空間,第一次獲取的bitmap實際上爲null,只是爲了讀取寬度和高度, 
  50.      *        第二次讀取的bitmap是根據比例壓縮過的圖像,第三次讀取的bitmap是所要的縮略圖。 
  51.      *     2. 縮略圖對於原圖像來講沒有拉伸,這裏使用了2.2版本的新工具ThumbnailUtils,使 
  52.      *        用這個工具生成的圖像不會被拉伸。 
  53.      * @param imagePath 圖像的路徑 
  54.      * @param width 指定輸出圖像的寬度 
  55.      * @param height 指定輸出圖像的高度 
  56.      * @return 生成的縮略圖 
  57.      */  
  58.     private Bitmap getImageThumbnail(String imagePath, int width, int height) {  
  59.         Bitmap bitmap = null;  
  60.         BitmapFactory.Options options = new BitmapFactory.Options();  
  61.         options.inJustDecodeBounds = true;  
  62.         // 獲取這個圖片的寬和高,注意此處的bitmap爲null  
  63.         bitmap = BitmapFactory.decodeFile(imagePath, options);  
  64.         options.inJustDecodeBounds = false// 設爲 false  
  65.         // 計算縮放比  
  66.         int h = options.outHeight;  
  67.         int w = options.outWidth;  
  68.         int beWidth = w / width;  
  69.         int beHeight = h / height;  
  70.         int be = 1;  
  71.         if (beWidth < beHeight) {  
  72.             be = beWidth;  
  73.         } else {  
  74.             be = beHeight;  
  75.         }  
  76.         if (be <= 0) {  
  77.             be = 1;  
  78.         }  
  79.         options.inSampleSize = be;  
  80.         // 重新讀入圖片,讀取縮放後的bitmap,注意這次要把options.inJustDecodeBounds 設爲 false  
  81.         bitmap = BitmapFactory.decodeFile(imagePath, options);  
  82.         // 利用ThumbnailUtils來創建縮略圖,這裏要指定要縮放哪個Bitmap對象  
  83.         bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,  
  84.                 ThumbnailUtils.OPTIONS_RECYCLE_INPUT);  
  85.         return bitmap;  
  86.     }  
  87.   
  88.     /** 
  89.      * 獲取視頻的縮略圖 
  90.      * 先通過ThumbnailUtils來創建一個視頻的縮略圖,然後再利用ThumbnailUtils來生成指定大小的縮略圖。 
  91.      * 如果想要的縮略圖的寬和高都小於MICRO_KIND,則類型要使用MICRO_KIND作爲kind的值,這樣會節省內存。 
  92.      * @param videoPath 視頻的路徑 
  93.      * @param width 指定輸出視頻縮略圖的寬度 
  94.      * @param height 指定輸出視頻縮略圖的高度度 
  95.      * @param kind 參照MediaStore.Images.Thumbnails類中的常量MINI_KIND和MICRO_KIND。 
  96.      *            其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96 
  97.      * @return 指定大小的視頻縮略圖 
  98.      */  
  99.     private Bitmap getVideoThumbnail(String videoPath, int width, int height,  
  100.             int kind) {  
  101.         Bitmap bitmap = null;  
  102.         // 獲取視頻的縮略圖  
  103.         bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);  
  104.         System.out.println("w"+bitmap.getWidth());  
  105.         System.out.println("h"+bitmap.getHeight());  
  106.         bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,  
  107.                 ThumbnailUtils.OPTIONS_RECYCLE_INPUT);  
  108.         return bitmap;  
  109.     }  
  110.       
  111. }  
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及以上版本使用,因爲其中使用了ThumbnailUtils這個類
 */
public class AndroidTestActivity extends Activity {
	private ImageView imageThumbnail;
	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.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. 使用較小的內存空間,第一次獲取的bitmap實際上爲null,只是爲了讀取寬度和高度,
	 *        第二次讀取的bitmap是根據比例壓縮過的圖像,第三次讀取的bitmap是所要的縮略圖。
	 *     2. 縮略圖對於原圖像來講沒有拉伸,這裏使用了2.2版本的新工具ThumbnailUtils,使
	 *        用這個工具生成的圖像不會被拉伸。
	 * @param imagePath 圖像的路徑
	 * @param width 指定輸出圖像的寬度
	 * @param height 指定輸出圖像的高度
	 * @return 生成的縮略圖
	 */
	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; // 設爲 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,注意這次要把options.inJustDecodeBounds 設爲 false
		bitmap = BitmapFactory.decodeFile(imagePath, options);
		// 利用ThumbnailUtils來創建縮略圖,這裏要指定要縮放哪個Bitmap對象
		bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
				ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
		return bitmap;
	}

	/**
	 * 獲取視頻的縮略圖
	 * 先通過ThumbnailUtils來創建一個視頻的縮略圖,然後再利用ThumbnailUtils來生成指定大小的縮略圖。
	 * 如果想要的縮略圖的寬和高都小於MICRO_KIND,則類型要使用MICRO_KIND作爲kind的值,這樣會節省內存。
	 * @param videoPath 視頻的路徑
	 * @param width 指定輸出視頻縮略圖的寬度
	 * @param height 指定輸出視頻縮略圖的高度度
	 * @param kind 參照MediaStore.Images.Thumbnails類中的常量MINI_KIND和MICRO_KIND。
	 *            其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
	 * @return 指定大小的視頻縮略圖
	 */
	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;
	}
	
}


main.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="圖片縮略圖" />  
  11.   
  12.     <ImageView  
  13.         android:id="@+id/image_thumbnail"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content" />  
  16.   
  17.     <TextView  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="視頻縮略圖" />  
  21.   
  22.     <ImageView  
  23.         android:id="@+id/video_thumbnail"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content" />  
  26.   
  27. </LinearLayout>  
<?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="圖片縮略圖" />

    <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>


 運行後的截圖爲:

 
發佈了19 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章