关于android获取视频缩略图的问题,大神进来

嘶吼的蚂蚁 2014-12-25 11:35:20

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
try {
mmr.setDataSource(path);
String title = new File(path).getName();
Bitmap bitMap = null;
String mataDataStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);

int seconds = 0;
if(mataDataStr!=null){
seconds = Integer.valueOf(mataDataStr);
}
if (seconds > 1) {
bitMap = mmr.getFrameAtTime(1 * 1000 * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
}
} catch (Exception e) {
e.printStackTrace();
}

先上代码。播放器侧边有一个listview列表,可以进入USB文件夹查看视频文件,listview中会加载视频文件的缩略图。

假设进入某个文件夹中有一个视频文件被损坏的,或者是其他类型的文件重命名成视频文件的。。获取缩略图的时候就报错了,导致播放器也停止了正在播放的视频文件。。

所以我想知道,有没有其他方法获取视频缩略图?经测试MediaMetadataRetriever ,MediaStore.Video.Thumbnails.getThumbnail等系统自带的方法获取,都会导致mediaplay停止播放。。。
...全文
607 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_35874950 2016-08-24
  • 打赏
  • 举报
回复
为什么我用getFrameAtTime后去的Bitmap值一直是null 有没有哪个大神给讲解下
一锅小青蛙 2015-11-19
  • 打赏
  • 举报
回复
这么高深的问题,CSDN没几个人知道!骚年,赶快换专业啊!~挨踢有风险
嘶吼的蚂蚁 2014-12-25
  • 打赏
  • 举报
回复
引用 2 楼 hefeng89505697 的回复:
private Bitmap getVideoThumb(String filePath) { Bitmap bitmap = null; MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { retriever.setDataSource(filePath); bitmap = retriever.getFrameAtTime(1000L, MediaMetadataRetriever.OPTION_PREVIOUS_SYNC); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } catch (RuntimeException ex) { ex.printStackTrace(); } finally { try { retriever.release(); } catch (RuntimeException ex) { ex.printStackTrace(); } } saveVideoThumb(bitmap); return bitmap; } private void saveVideoThumb(Bitmap thumb) { thumbPath = filePath + System.currentTimeMillis() + Suffix.JPG; File img = new File(thumbPath); if (!img.exists()) { try { img.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } FileOutputStream fos; try { fos = new FileOutputStream(thumbPath); thumb.compress(Bitmap.CompressFormat.JPEG, 100, fos);// 把数据写入文件 } catch (FileNotFoundException e) { e.printStackTrace(); } }
毛毛,我都说了。用这个方法获取缩略图。会导致播放器停止播放当前的视频
何阿毛 2014-12-25
  • 打赏
  • 举报
回复
也只有大爷关心你这个屌丝。
何阿毛 2014-12-25
  • 打赏
  • 举报
回复
private Bitmap getVideoThumb(String filePath) { Bitmap bitmap = null; MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { retriever.setDataSource(filePath); bitmap = retriever.getFrameAtTime(1000L, MediaMetadataRetriever.OPTION_PREVIOUS_SYNC); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } catch (RuntimeException ex) { ex.printStackTrace(); } finally { try { retriever.release(); } catch (RuntimeException ex) { ex.printStackTrace(); } } saveVideoThumb(bitmap); return bitmap; } private void saveVideoThumb(Bitmap thumb) { thumbPath = filePath + System.currentTimeMillis() + Suffix.JPG; File img = new File(thumbPath); if (!img.exists()) { try { img.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } FileOutputStream fos; try { fos = new FileOutputStream(thumbPath); thumb.compress(Bitmap.CompressFormat.JPEG, 100, fos);// 把数据写入文件 } catch (FileNotFoundException e) { e.printStackTrace(); } }
嘶吼的蚂蚁 2014-12-25
  • 打赏
  • 举报
回复
没人么?
嘶吼的蚂蚁 2014-12-25
  • 打赏
  • 举报
回复
获取缩略图的时候,直接就报这个error
嘶吼的蚂蚁 2014-12-25
  • 打赏
  • 举报
回复
人呢?

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧