浏览 14971 次
锁定老帖子 主题:android获取图片和视频的缩略图
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-08-27
获取图片缩略图:
byte[] imageByte=getImageFromURL(urlPath.trim());
//以下是把图片转化为缩略图再加载
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length); //此时返回bitmap为空
options.inJustDecodeBounds = false;
int be = (int)(options.outHeight / (float)200);
if (be <= 0){
be = 1;
}
options.inSampleSize = be;
return BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options); //返回缩略图
获取视频缩略图:
/** * 根据视频Uri地址取得指定的视频缩略图 * @param cr * @param uri 本地视频Uri标示 * @return 返回bitmap类型数据 */ 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; }/** * 根据视频在手机中的地址路径取得指定的视频缩略图 * @param cr * @param fileName 本地视频地址 * @return 返回bitmap类型数据 */
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; } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-08-30
缩略图没有看明白
|
|
返回顶楼 | |
发表时间:2011-08-31
也就是获取第一帧
|
|
返回顶楼 | |
发表时间:2011-09-13
纠正一个错误:在第一框代码获取图片缩略图中的第5行
BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length); 应该改为 BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options); |
|
返回顶楼 | |
发表时间:2011-09-18
学习了!
但是BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length); 应该改为 BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options); |
|
返回顶楼 | |
发表时间:2011-09-19
最后修改:2011-09-19
cwjtojava 写道 学习了!
但是BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length); 应该改为 BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options); 呵呵,在你之前一个评论我已经纠正了,注意看,谢谢 |
|
返回顶楼 | |
发表时间:2011-12-11
怎么用啊?!?!!????
|
|
返回顶楼 | |
发表时间:2011-12-12
lauphai 写道 怎么用啊?!?!!????
这位兄台,方法都给你了,还不会用吗? |
|
返回顶楼 | |
发表时间:2011-12-25
ContentResolver cr 是什么,怎么获取
|
|
返回顶楼 | |
发表时间:2011-12-26
pgmsoul 写道 ContentResolver cr 是什么,怎么获取
直接在activity页面通过 getContentResolver() 方法获取 |
|
返回顶楼 | |