下面列出了android.media.ThumbnailUtils#createVideoThumbnail ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onContentLoaded(File file, String contentType) {
targetFile = file;
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(targetFile.getAbsolutePath(),
MediaStore.Video.Thumbnails.MINI_KIND);
thumbUrl = FilesUtils.storeThumb(thumb);
checkContentType(contentType);
if (checkIsHaveMetaData(Uri.fromFile(file))){
finishAddVideoToDB();
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//Uri imageUri = Uri.fromFile(new File(list.get(position)));// For files on device
File f = new File(list.get(position));
Bitmap bitmap;
if (f.getAbsolutePath().endsWith("mp4")) {
((Holder) holder).play.setVisibility(View.VISIBLE);
bitmap = ThumbnailUtils.createVideoThumbnail(f.getAbsolutePath(), MediaStore.Video.Thumbnails.MINI_KIND);
} else {
((Holder) holder).play.setVisibility(View.GONE);
bitmap = new BitmapDrawable(context.getResources(), f.getAbsolutePath()).getBitmap();
}
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
final float roundPx = (float) bitmap.getWidth() * 0.06f;
roundedBitmapDrawable.setCornerRadius(roundPx);
((Holder) holder).iv.setImageDrawable(roundedBitmapDrawable);
((Holder) holder).iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(f.getAbsolutePath()));
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.setDataAndType(Uri.parse(f.getAbsolutePath()), Files.probeContentType(f.toPath()));
} else {
intent.setData(Uri.parse(f.getAbsolutePath()));
}
} catch (IOException e) {
e.printStackTrace();
}
context.startActivity(intent);
}
});
/*Bitmap scaled = com.fxn.utility.Utility.getScaledBitmap(
500f, com.fxn.utility.Utility.rotate(d,list.get(position).getOrientation()));*/
}
public static Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind) {
Bitmap bitmap = null;
// 获取视频的缩略图
bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
if(bitmap!= null){
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
}
return bitmap;
}
public void generateVideoThmb(String srcFilePath, File destFile) {
try {
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(srcFilePath, 120);
FileOutputStream out = new FileOutputStream(destFile);
ThumbnailUtils.extractThumbnail(bitmap, 200, 200).compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@TargetApi(Build.VERSION_CODES.FROYO)
private InputStream getVideoThumbnailStream(String filePath) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
Bitmap bitmap = ThumbnailUtils
.createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
if (bitmap != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, bos);
return new ByteArrayInputStream(bos.toByteArray());
}
}
return null;
}
public void setVideoPath(String path) {
boolean isValid = false;
boolean isFileExist = false;
mVideoPathValue = path;
do {
if(path == null || path == "") break;
isFileExist = isFileExist(path);
if(isFileExist) {
try {
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(path);
mWidth = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
mHeight = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
this.setSummary(" Video Path : " + path + " < " + mWidth + " x " + mHeight + " >");
persistString((String) path);
if (thumb != null) {
setPreviewBitmap(thumb);
} else {
setPreviewResource(mContext.getResources().getDrawable(R.drawable.icon_unknown_video_fmt));
}
isValid = true;
}
catch (Exception e) {
e.printStackTrace();
}
}
} while(false);
if(!isValid) {
if(!isFileExist) {
this.setSummary(" File not exist or permission denied. Click to select a new \".mp4 \" file...");
}
else {
this.setSummary(" Fail to parse bitmap in this file. Click to select a new \".mp4 \" file...");
}
persistString("");
if(isEnabled()) {
this.setBackgroundColor(Color.rgb(170, 30, 30));
}
else {
this.setBackgroundColor(Color.TRANSPARENT);
}
}
else {
this.setBackgroundColor(Color.TRANSPARENT);
}
if(mView != null) {
super.onBindView(mView); // use base class's oBindView to avoid recursive.
}
}
public static Bitmap createVideoThumbnail(String filePath) {
return ThumbnailUtils.createVideoThumbnail(filePath.replace("file://", ""), 1);
}
private Bitmap getThumbnail() {
if (filename == null) return null;
return ThumbnailUtils.createVideoThumbnail(filename, Thumbnails.FULL_SCREEN_KIND);
}
/** 创建视频缩略图,返回缩略图文件路径 */
public String createVideoThumbnail(String filePath, String fileName){
Bitmap videoThumb = ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MINI_KIND); //关键代码!!
return saveBitmap(videoThumb, fileName); //注:saveBitmap方法为保存图片并返回路径的private方法
}
public static Bitmap createVideoThumbnail(String path) {
return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
}
private static Bitmap getVideoFrame(String path) {
return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
}
private static Bitmap getPreview(File file) {
final boolean isImage = MimeTypes.isPicture(file);
final boolean isVideo = MimeTypes.isVideo(file);
final boolean isApk = file.getName().endsWith(".apk");
Bitmap mBitmap = null;
String path = file.getAbsolutePath();
if (isImage) {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
o.inJustDecodeBounds = false;
if (o.outWidth != -1 && o.outHeight != -1) {
final int originalSize = (o.outHeight > o.outWidth) ? o.outWidth
: o.outHeight;
o.inSampleSize = originalSize / mWidth;
}
mBitmap = BitmapFactory.decodeFile(path, o);
addBitmapToMemoryCache(path, mBitmap);
return mBitmap;
} else if (isVideo) {
mBitmap = ThumbnailUtils.createVideoThumbnail(path,
MediaStore.Video.Thumbnails.MICRO_KIND);
addBitmapToMemoryCache(path, mBitmap);
return mBitmap;
} else if (isApk) {
//Log.w("ruijie", path);
final PackageInfo packageInfo = pm.getPackageArchiveInfo(path,
PackageManager.GET_ACTIVITIES);
if (packageInfo != null) {
final ApplicationInfo appInfo = packageInfo.applicationInfo;
if (appInfo != null) {
appInfo.sourceDir = path;
appInfo.publicSourceDir = path;
final Drawable icon = appInfo.loadIcon(pm);
if (icon != null) {
mBitmap = ((BitmapDrawable) icon).getBitmap();
}
}
} else {
// load apk icon from /res/drawable/..
mBitmap = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.type_apk);
}
addBitmapToMemoryCache(path, mBitmap);
return mBitmap;
}
return null;
}
/**
* get the thumbnail for image video and so on
* <b>note that it will take much mTime and block</b>
* */
public static Bitmap getThumbnail(String path,int size)
{
String extensionString = FileTool.getExtension(path);
Bitmap thumbnailBitmap = null;
if(isStringIn(extensionString,videoExtension))
{
thumbnailBitmap = ThumbnailUtils.createVideoThumbnail(path,size);
}
if(isStringIn(extensionString,imageExtension))
{
int width=0,height=0;
switch (size)
{
case MICRO_KIND:
width = 96;
height = 96;
break;
case MINI_KIND:
width = 512;
height = 384;
break;
case FULL_SCREEN_KIND:
width = 700;
height = 1200;
break;
}
thumbnailBitmap = getImageThumbnail(path,width,height);
}
if(extensionString.equalsIgnoreCase("apk"))
{
if(mContext!=null) {
Drawable drawable = getApkIcon(mContext, path);
if(drawable!=null)
{
thumbnailBitmap = drawableToBitamp(drawable);
}
}
}
return thumbnailBitmap;
}
private Bitmap getVideoThumbnail(String videoPath) {
return ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Video.Thumbnails.MINI_KIND);
}
@Override
protected Bitmap doInBackground(Object... params) {
return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
}
private static Bitmap getVideoFrame(String path) {
return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
}
/**
* get the thumbnail for image video and so on
* <b>note that it will take much mTime and block</b>
* */
public static Bitmap getThumbnail(String path,int size)
{
String extensionString = FileTool.getExtension(path);
Bitmap thumbnailBitmap = null;
if(isStringIn(extensionString,videoExtension))
{
thumbnailBitmap = ThumbnailUtils.createVideoThumbnail(path,size);
}
if(isStringIn(extensionString,imageExtension))
{
int width=0,height=0;
switch (size)
{
case MICRO_KIND:
width = 96;
height = 96;
break;
case MINI_KIND:
width = 512;
height = 384;
break;
case FULL_SCREEN_KIND:
width = 700;
height = 1200;
break;
}
thumbnailBitmap = getImageThumbnail(path,width,height);
}
if(extensionString.equalsIgnoreCase("apk"))
{
if(mContext!=null) {
Drawable drawable = getApkIcon(mContext, path);
if(drawable!=null)
{
thumbnailBitmap = drawableToBitamp(drawable);
}
}
}
return thumbnailBitmap;
}
public static Bitmap getVideoThumbnail(String filePath) {
return ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.MINI_KIND);
}
/**
* 获取视频的缩略图 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。
* 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。
*
* @param videoPath
* 视频的路径
* @param width
* 指定输出视频缩略图的宽度
* @param height
* 指定输出视频缩略图的高度度
* @param kind
* 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。
* 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
* @return 指定大小的视频缩略图
*/
public static Bitmap getVideoThumbnail(String videoPath, int width,
int height, int kind) {
Bitmap bitmap = null;
bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
if (width > 0 && height > 0) {
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
}
return bitmap;
}
/**
* 获取视频的缩略图
* 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。
* 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。
*
* @param videoPath 视频的路径
* @param width 指定输出视频缩略图的宽度
* @param height 指定输出视频缩略图的高度度
* @param kind 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。
* 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
* @return 指定大小的视频缩略图
*/
private Bitmap createVideoThumbBitmap(String videoPath, int width, int height, int kind)
{
Bitmap bitmap = null;
// 获取视频的缩略图
bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}