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

本文介绍如何使用Java代码生成图片和视频的缩略图。对于图片,通过设置采样率来创建缩略图;对于视频,则通过ContentResolver和MediaStore API获取视频缩略图。文中提供了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取图片缩略图:

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

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. }
		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类型数据

*/

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. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值