论坛首页 移动开发技术论坛

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

浏览 14934 次
精华帖 (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;
		}
	
 
   发表时间:2011-08-30  
缩略图没有看明白
0 请登录后投票
   发表时间:2011-08-31  
也就是获取第一帧
0 请登录后投票
   发表时间:2011-09-13  
纠正一个错误:在第一框代码获取图片缩略图中的第5行
BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length); 应该改为
BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options);
0 请登录后投票
   发表时间:2011-09-18  
学习了!
但是BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length); 应该改为 BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options);
0 请登录后投票
   发表时间:2011-09-19   最后修改:2011-09-19
cwjtojava 写道
学习了!
但是BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length); 应该改为 BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options);

呵呵,在你之前一个评论我已经纠正了,注意看,谢谢
0 请登录后投票
   发表时间:2011-12-11  
怎么用啊?!?!!????
0 请登录后投票
   发表时间:2011-12-12  
lauphai 写道
怎么用啊?!?!!????

这位兄台,方法都给你了,还不会用吗?
0 请登录后投票
   发表时间:2011-12-25  
ContentResolver cr 是什么,怎么获取
0 请登录后投票
   发表时间:2011-12-26  
pgmsoul 写道
ContentResolver cr 是什么,怎么获取

直接在activity页面通过 getContentResolver() 方法获取
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics