我需要获取视频文件的第一帧(它可以在sdcard、缓存目录或应用程序目录中)。 我的应用程序中有android.media包,其中包含MediaMetadataRetriever类。 为了将第一帧提取到位图中,我使用以下代码:
public static Bitmap getVideoFrame(Context context, Uri videoUri) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
retriever.setDataSource(context, videoUri);
return retriever.captureFrame();
} catch (IllegalArgumentException ex) {
throw new RuntimeException();
} catch (RuntimeException ex) {
throw new RuntimeException();
} finally {
retriever.release();
}
}
但是这不起作用。当我设置数据源时,它会抛出异常(java.lang.RuntimeException:setDataSource失败:状态= 0x80000000)。您知道如何使此代码工作吗?或者,您是否有任何类似的(简单)解决方案,而无需使用ffmpeg或其他外部库?videoUri是有效的URI(媒体播放器可以从该URI播放视频)。