android为视频、音乐、图片等生成缩略图

1、Video
对于视频,取第一帧作为缩略图,也就是如何从filePath获得一个Bitmap对象。 

private Bitmap createVideoThumbnail(String filePath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
            retriever.setDataSource(filePath);
            bitmap = retriever.captureFrame();
        } catch(IllegalArgumentException ex) {
            // Assume this is a corrupt video file
        } catch (RuntimeException ex) {
            // Assume this is a corrupt video file.
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
                // Ignore failures while cleaning up.
            }
        }
        return bitmap;
    }Android供给了MediaMetadataRetriever,由JNI(media_jni)实现。
看得出MediaMetadataRetriever首要有两个功能:MODE_GET_METADATA_ONLY和MODE_CAPTURE_FRAME_ONLY
这里设mode为MODE_CAPTURE_FRAME_ONLY,调用captureFrame取得一帧。
别的还有两个办法可以用:
extractMetadata 提取文件信息,ARTIST、DATE、YEAR、DURATION、RATING、FRAME_RATE、VIDEO_FORMAT
和extractAlbumArt 提取专辑信息,这个下面的音乐文件可以用到。

2、Music
对于音乐,取得AlbumImage作为缩略图,还是用MediaMetadataRetriever 

private Bitmap createAlbumThumbnail(String filePath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setMode(MediaMetadataRetriever.MODE_GET_METADATA_ONLY);
            retriever.setDataSource(filePath);
            byte[] art = retriever.extractAlbumArt();
            bitmap = BitmapFactory.decodeByteArray(art, 0, art.length);
        } catch(IllegalArgumentException ex) {
        } catch (RuntimeException ex) {
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
                // Ignore failures while cleaning up.
            }
        }
        return bitmap;
    }

retriever.extractAlbumArt()获得的是byte数组,还须要一步用BitmapFactory编码获得Bitmap对象。

3、Image
图片就很简单了         Bitmap bm = null;

        Options op = new Options();

        op.inSampleSize = inSampleSize;

        op.inJustDecodeBounds = false;

        bm = BitmapFactory.decodeFile(mFile.getPath(), op);

复制代码能直接获得Bitmap对象,把图片缩小到合适大小就OK。
同样上方的Video和Music,retrive到Bitmap后也须要缩小处理惩罚。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值