MediaMetadataRetriever media = new MediaMetadataRetriever();
media.setDataSource(videoPath);
Bitmap bitmap = media.getFrameAtTime();
File videoFile = new File(videoPath);
File parentFile = videoFile.getParentFile();
String videoName = videoFile.getName();
String picName = videoName;
if (videoName.contains(".")) {
picName = videoName.split("\\.")[0];
}
picName += ".jpeg";
File picFile = new File(parentFile, picName);
try {
FileOutputStream fileOutputStream = new FileOutputStream(picFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, fileOutputStream);
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.i(TAG, "getVideoThumbnail: 生成图片地址为:" + picFile.getAbsolutePath());
String s = media.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
try {
int i = Integer.parseInt(s);
videoLength = i / 1000;
if (i % 1000 != 0) {
videoLength++;
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
media.release();
网友评论