- 首先在vender/mediatek/properties/packags/apps/FileManager/src/com/mediatek/filemanager/IconManager.java中导入两个类
import android.media.ThumbnailUtils; import android.provider.MediaStore.Video.Thumbnails;
- 在getIcon方法中添加
Bitmap icon=null;//方法已经定义,不需添加 //显示图片缩略图 if(mimeType.startsWith("image/")){ Bitmap bitmap=BitmapFactory.decodeFile(fileInfo.getFileAbsolutePath());//fileInfo.getFileAbsolutePath()方法得到文件路径的字符串形式 icon=ThumbnailUtils.extractThumbnail(bitmap,96,96,ThumbnailUtils.OPTIONS_RECYCLE_INPUT); } //显示视频第一帧缩略图 if(mimeType.startsWith("video/")){ icon=ThumbnailUtils.createVideoThumbnail(fileInfo.getFileAbsolutePath(),Thumbnails.MICRO_KIND); } //针对HTML和HTM文件后缀进行判别显示相应的图片 int iconId=getDrawableId(service,mimeType);//已经定义,无需添加 if(mimeType.startsWith("text/html")){ if(fileInfo.getFileAbsolutePath().endsWith("html")){ iconId=R.drawable.fm_html; } else{ iconId=R.drawable.fm_htm; } }
- 在不知道以上方法的时候,以下是加载图片缩略图在网上找到的另外一种实现方式
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } // 如果是放大图片,filter决定是否平滑,如果是缩小图片,filter无影响 private static Bitmap createScaleBitmap(Bitmap src, int dstWidth, int dstHeight) { Bitmap dst = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, false); if (src != dst) { // 如果没有缩放,那么不回收 src.recycle(); // 释放Bitmap的native像素数组 } return dst; } // 从Resources中加载图片 public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // 读取图片长宽 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // 计算inSampleSize options.inJustDecodeBounds = false; Bitmap src = BitmapFactory.decodeResource(res, resId, options); // 载入一个稍大的缩略图 return createScaleBitmap(src, reqWidth, reqHeight); // 进一步得到目标大小的缩略图 } // 从sd卡上加载图片 public static Bitmap decodeSampledBitmapFromFd(String pathName, int reqWidth, int reqHeight) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(pathName, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; Bitmap src = BitmapFactory.decodeFile(pathName, options); return createScaleBitmap(src, reqWidth, reqHeight); }
- 将以上方法添加到类IconManager中,同时在getIcon方法中添加调用以上方法的语句
if(mimeType.startsWith("image/")){ icon=decodeSampleBitmapFromFd(fileInfo.getFileabsolutePath(),96,96); }
- 此方法可以实现图片缩略图的显示,但是对图片进行删除操作时,会出现报错,程序终止退出,报错内容如下
- 通过报错信息我们可以发现错误出现在下面这句代码上,应该是在删除图片后,src为null,该方法不能解析空资源,会抛出文件找不到异常(FileNotFoundException)因此报错
Bitmap dst = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, false);
- 我的解决方法是对资源src进行判断,如果资源为空则不进行资源解析,即不执行上面的代码,修改如下
private static Bitmap createScaleBitmap(Bitmap src, int dstWidth, int dstHeight) { Bitmap dst=null; if(src!=null){ dst = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, false); } if (src != dst) { src.recycle(); } return dst; }
- 结果成功
在文件管理器中显示图片的缩略图和视频第一帧的缩略图
最新推荐文章于 2024-07-12 03:22:31 发布