`
hududumo
  • 浏览: 239278 次
文章分类
社区版块
存档分类
最新评论

android获取图片和视频的缩略图

 
阅读更多

获取图片缩略图:

Java代码 复制代码 收藏代码
  1. byte[] imageByte=getImageFromURL(urlPath.trim());
  2. //以下是把图片转化为缩略图再加载
  3. BitmapFactory.Options options = new BitmapFactory.Options();
  4. options.inJustDecodeBounds = true;
  5. BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,<span style="background-color: rgb(255, 255, 255);">options </span>); <span style="line-height: 25px; font-size: 14px; white-space: normal;"> //此时返回bitmap为空 </span>
Java代码 复制代码 收藏代码
  1. options.inJustDecodeBounds = false;
  2. int be = (int)(options.outHeight / (float)200);
  3. if (be <= 0){
  4. be = 1;
  5. }
  6. options.inSampleSize = be;
  7. return BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options); //返回缩略图

获取视频缩略图:

/**

* 根据视频Uri地址取得指定的视频缩略图

* @param cr

* @param uri 本地视频Uri标示

* @return 返回bitmap类型数据

*/

public static Bitmap getVideoThumbnail(ContentResolver cr, Uri uri) {

Java代码 复制代码 收藏代码
  1. Bitmap bitmap = null;
  2. BitmapFactory.Options options = new BitmapFactory.Options();
  3. options.inDither = false;
  4. options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  5. Cursor cursor = cr.query(uri,new String[] { MediaStore.Video.Media._ID }, null, null, null);
  6. if (cursor == null || cursor.getCount() == 0) {
  7. return null;
  8. }
  9. cursor.moveToFirst();
  10. String videoId = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media._ID)); //image id in image table.s
  11. if (videoId == null) {
  12. return null;
  13. }
  14. cursor.close();
  15. long videoIdLong = Long.parseLong(videoId);
  16. bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, videoIdLong,Images.Thumbnails.MICRO_KIND, options);
  17. return bitmap;
  18. }

/**

* 根据视频在手机中的地址路径取得指定的视频缩略图

* @param cr

* @param fileName 本地视频地址

* @return 返回bitmap类型数据

*/

Java代码 复制代码 收藏代码
  1. public static Bitmap getVideoThumbnail(ContentResolver cr, Uri uri) {
  2. Bitmap bitmap = null;
  3. BitmapFactory.Options options = new BitmapFactory.Options();
  4. options.inDither = false;
  5. options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  6. Cursor cursor = cr.query(uri,new String[] { MediaStore.Video.Media._ID }, null, null, null);
  7. if (cursor == null || cursor.getCount() == 0) {
  8. return null;
  9. }
  10. cursor.moveToFirst();
  11. String videoId = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media._ID)); //image id in image table.s
  12. if (videoId == null) {
  13. return null;
  14. }
  15. cursor.close();
  16. long videoIdLong = Long.parseLong(videoId);
  17. bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, videoIdLong,Images.Thumbnails.MICRO_KIND, options);
  18. return bitmap;
  19. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics