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

获取图片缩略图:

 

 

 

byte[] imageByte=getImageFromURL(urlPath.trim()); 
			//以下是把图片转化为缩略图再加载
			BitmapFactory.Options options = new BitmapFactory.Options(); 
			options.inJustDecodeBounds = true;
			BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options );    //此时返回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;
		}
	

你可能感兴趣的:(android,缩略图)