android获取多媒体文件的缩…
来源:互联网 发布:keyshot 6 mac 百度云 编辑:程序博客网 时间:2024/05/20 09:45
原文地址:android获取多媒体文件的缩略图作者:指间流砂丶
1、Video
对于视频,取第一帧作为缩略图,也就是怎样从filePath得到一个Bitmap对象。
private Bitmap createVideoThumbnail(String filePath) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = newMediaMetadataRetriever();
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 = newMediaMetadataRetriever();
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后也需要缩小处理。
对于视频,取第一帧作为缩略图,也就是怎样从filePath得到一个Bitmap对象。
private Bitmap createVideoThumbnail(String filePath) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = newMediaMetadataRetriever();
try {
retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
retriever.setDataSource(filePath);
bitmap = retriever.captureFrame();
} catch(IllegalArgumentException
// 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 = newMediaMetadataRetriever();
try {
retriever.setMode(MediaMetadataRetriever.MODE_GET_METADATA_ONLY);
retriever.setDataSource(filePath);
byte[] art = retriever.extractAlbumArt();
bitmap = BitmapFactory.decodeByteArray(art, 0, art.length);
} catch(IllegalArgumentException
} 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 0
- android获取多媒体文件的缩…
- android获取多媒体文件的缩略图
- Android 获取多媒体文件的缩略图
- Android中多媒体文件、文档以及各类文件的获取
- Android 获取SD卡上的多媒体文件
- android如何获取SD卡上的多媒体文件
- Android 多媒体文件获取工具类
- Android下raw文件夹下获取不到多媒体文件的缩略图
- Android中利用正则表达式获取多媒体文件URL
- android 隐藏自己的多媒体文件
- Android 播放多媒体文件
- 如何获取多媒体文件信息
- Android通过.nomedia文件禁止多媒体库扫描指定文件夹下的多媒体文件
- android获取多媒体库的视频、音频、图片
- Android多媒体--网络文件播放
- Android扫描多媒体文件剖析
- Android之播放多媒体文件
- Android多媒体文件扫描流程
- ubuntu12.04系统设置及软件…
- 10个你值得拥有的Vim配色方…
- github相关配置
- Dll导入导出
- 面试的几个回答技巧
- android获取多媒体文件的缩…
- Eclipse CDT JNI OPENCV Windows NDK-gdb两步搞定调试
- as2.0步进教程2--编程基础
- oracle中的常用函数
- as2.0步进教程3--as基本语法
- as2.0步进教程4--运算符
- as2.0步进教程5--条件语句
- as2.0步进教程6--循环语句
- VC++,掀起你的盖头来 ——谈VC++对象模型