搜索
 找回密码
 立即注册

简单一步 , 微信登陆

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

作者:liuwei | 时间:2016-7-7 12:18:44 | 阅读:5156| 只看该作者
2.3以上版本才能使用
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;

        Opti** op
=new Opti**();

        op.inSampleSize
= inSampleSize;

        op.inJustDecodeBounds
=false;

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

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

收藏
收藏0
分享
分享
点赞
点赞0
反对
反对0
回复

使用道具 举报

大神点评2

沙发#
hongtian 发表于:2017-1-13 15:32:40
努力奋斗!
回复

使用道具 举报

板凳#
Jack.Lin 发表于:2017-2-6 15:34:58
好东西,收藏下。
该会员没有填写今日想说内容.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册
手机版