获取缩略图的几种方式

//获取视频格式缩略图

 

1、通过字符串格式的 Uri

 

private Bitmap getVideoThumbnail(String uri, int width, int height, int kind) {

     Bitmap bitmap = null;

     bitmap = ThumbnailUtils.createVideoThumbnail(uri, kind);

     bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,

     ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

 

     return bitmap;

}

调用:

getVideoThumbnail(uri, 200,200, MediaStore.Images.Thumbnails.MICRO_KIND);

 

//获取图片的缩略图

 

public Bitmap getBitmapByPath(String localImagePath) {

int width = 30;

int addedScaling = 30;

 

if (TextUtils.isEmpty(localImagePath)) {

 

  return null;

}

 

Bitmap temBitmap = null;

 

try {

 

  BitmapFactory.Options outOptions = new BitmapFactory.Options();

 

// 设置该属性为true,不加载图片到内存,只返回图片的宽高到options中。

  outOptions.inJustDecodeBounds = true;

 

// 加载获取图片的宽高

  BitmapFactory.decodeFile(localImagePath, outOptions);

 

  int height = outOptions.outHeight;

 

  if (outOptions.outWidth > width) {

// 根据宽设置缩放比例

  outOptions.inSampleSize = outOptions.outWidth / width + 1+ addedScaling;

  outOptions.outWidth = width;

 

// 计算缩放后的高度

  height = outOptions.outHeight / outOptions.inSampleSize;

  outOptions.outHeight = height;

}

 

// 重新设置该属性为false,加载图片返回

  outOptions.inJustDecodeBounds = false;

  temBitmap = BitmapFactory.decodeFile(localImagePath, outOptions);

 

} catch (Throwable t) {

 

  t.printStackTrace();

}

 

  return temBitmap;

}

 

你可能感兴趣的:(获取缩略图)