在文件管理器中显示图片的缩略图和视频第一帧的缩略图

  • 首先在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;
        }
     

  • 结果成功
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 vant 的 Upload 组件实现文件上传,同时使用 video.js 库获取视频第一作为缩略图。具体实现可以参考以下代码: <template> <van-uploader :after-read="afterRead" :before-read="beforeRead" :preview-options="{showIndicators: false}" :max-count="1" accept="video/*" capture="camera" multiple :preview-image="false" :preview-full-image="false" :preview-cover="false" > <van-icon name="photograph" /> </van-uploader> </template> <script> import videojs from 'video.js' import 'video.js/dist/video-js.css' export default { methods: { async beforeRead(file) { // 判断是否为视频文件 if (file.type.indexOf('video') !== 0) { this.$toast('请选择视频文件') return false } }, async afterRead(file) { // 创建 video 元素 const video = document.createElement('video') video.src = URL.createObjectURL(file) video.preload = 'metadata' video.onloadedmetadata = async () => { // 获取视频第一作为缩略图 const canvas = document.createElement('canvas') canvas.width = video.videoWidth canvas.height = video.videoHeight canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height) const thumbnail = canvas.toDataURL('image/jpeg') // 上传视频缩略图 const formData = new FormData() formData.append('video', file) formData.append('thumbnail', thumbnail) // 发送上传请求 const response = await this.$axios.post('/api/upload', formData) this.$toast('上传成功') } // 初始化 video.js videojs(video) }, }, } </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值