欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年04月15日 12:02:03   作者:jopen  
這篇文章主要介紹了Android編程獲取圖片和視頻縮略圖的方法,結(jié)合實例形式分析了Android圖形圖像處理所涉及的常用函數(shù)與使用技巧,需要的朋友可以參考下

本文實例講述了Android編程獲取圖片和視頻縮略圖的方法。分享給大家供大家參考,具體如下:

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

1. createVideoThumbnail

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

2. extractThumbnail

static Bitmap extractThumbnail(Bitmap source, int width, int height, int options)
//直接對Bitmap進行縮略操作
//最后一個參數(shù)定義為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)); //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;
}

獲得指定目錄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;
/**
* 獲取圖片和視頻的縮略圖
* 這兩個方法必須在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));
   }
   /**
   * 根據(jù)指定的圖像路徑和大小來獲取縮略圖
   * 此方法有兩點好處:
   *   1. 使用較小的內(nèi)存空間,第一次獲取的bitmap實際上為null,只是為了讀取寬度和高度,
   *    第二次讀取的bitmap是根據(jù)比例壓縮過的圖像,第三次讀取的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; // 設(shè)為 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 設(shè)為 false
   bitmap = BitmapFactory.decodeFile(imagePath, options);
   // 利用ThumbnailUtils來創(chuàng)建縮略圖,這里要指定要縮放哪個Bitmap對象
   bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
   return bitmap;
   }
   /**
   * 獲取視頻的縮略圖
   * 先通過ThumbnailUtils來創(chuàng)建一個視頻的縮略圖,然后再利用ThumbnailUtils來生成指定大小的縮略圖。
   * 如果想要的縮略圖的寬和高都小于MICRO_KIND,則類型要使用MICRO_KIND作為kind的值,這樣會節(jié)省內(nèi)存。
   * @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;
   }
}

布局:

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

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • 詳解Android開發(fā)技巧之PagerAdapter實現(xiàn)類的封裝

    詳解Android開發(fā)技巧之PagerAdapter實現(xiàn)類的封裝

    這篇文章主要介紹了詳解Android開發(fā)技巧之PagerAdapter實現(xiàn)類的封裝,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Android Service自啟動注意事項分析

    Android Service自啟動注意事項分析

    這篇文章主要介紹了Android Service自啟動注意事項,結(jié)合實例分析了Android Service自啟動過程中屬性設(shè)置的相關(guān)技巧,需要的朋友可以參考下
    2016-03-03
  • Android SharedPreferences實現(xiàn)記住密碼和自動登錄

    Android SharedPreferences實現(xiàn)記住密碼和自動登錄

    這篇文章主要為大家詳細(xì)介紹了Android SharedPreferences實現(xiàn)記住密碼和自動登錄,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Android開發(fā)之開關(guān)按鈕控件ToggleButton簡單用法示例

    Android開發(fā)之開關(guān)按鈕控件ToggleButton簡單用法示例

    這篇文章主要介紹了Android開發(fā)之開關(guān)按鈕控件ToggleButton簡單用法,結(jié)合實例形式分析了Android開關(guān)按鈕控件ToggleButton的相關(guān)xml布局與調(diào)用操作技巧,需要的朋友可以參考下
    2017-12-12
  • android 定時啟動\取消小例子

    android 定時啟動\取消小例子

    本文為大家講解下android實現(xiàn)定時啟動\取消的具體實現(xiàn)方式,感興趣的朋友可以參考下哈
    2013-06-06
  • Flutter?Ping檢查服務(wù)器通訊信號強度實現(xiàn)步驟

    Flutter?Ping檢查服務(wù)器通訊信號強度實現(xiàn)步驟

    這篇文章主要為大家介紹了Flutter?Ping檢查服務(wù)器通訊信號強度實現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Android AS為xutils添加依賴過程圖解

    Android AS為xutils添加依賴過程圖解

    這篇文章主要介紹了Android AS為xutils添加依賴過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • Android集成微信支付功能

    Android集成微信支付功能

    這篇文章主要為大家詳細(xì)介紹了Android集成微信支付功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android掃描和生成二維碼

    Android掃描和生成二維碼

    這篇文章主要為大家詳細(xì)介紹了Android掃描二維碼和生成二維碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android 中Banner的使用詳解

    Android 中Banner的使用詳解

    這篇文章主要介紹了Android 中Banner的使用詳解,需要的朋友可以參考下
    2017-06-06

最新評論