Vla*_*kin 44 video android thumbnails
查询MediaStore.Video.Media.EXTERNAL_CONTENT_URI仅返回视频/sdcard/DCIM/100MEDIA
但我想在我的/sdcard/Android/data/mypackage/files文件夹中获取视频缩略图.可能吗 ?
这是我的代码的一部分:
ContentResolver cr = getContentResolver();
String[] proj = {
BaseColumns._ID
};
Cursor c = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
if (c.moveToFirst()) {
do
{
int id = c.getInt(0);
Bitmap b = MediaStore.Video.Thumbnails.getThumbnail(cr, id, MediaStore.Video.Thumbnails.MINI_KIND, null);
Log.d("*****My Thumbnail*****", "onCreate bitmap " + b);
ImageView iv = (ImageView) findViewById(R.id.img_thumbnail);
iv.setImageBitmap(b);
}
while( c.moveToNext() );
}
c.close();
Run Code Online (Sandbox Code Playgroud)
Mat*_*lis 117
如果您使用的是android-8(Froyo)或更高版本,则可以使用ThumbnailUtils.createVideoThumbnail:
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path,
MediaStore.Images.Thumbnails.MINI_KIND);
Run Code Online (Sandbox Code Playgroud)
Ajj*_*jji 22
使用Glide它将在异步中获取缩略图.
Glide.with(context)
.load(videoFilePath) // or URI/path
.into(imgView); //imageview to set thumbnail to
Run Code Online (Sandbox Code Playgroud)
从视频中获取缩略图的 3 种方法:
最好的方法是使用Glide。它将在后台完成所有工作,将缩略图加载到 ImageView 中,甚至可以在加载时显示动画。它可以与 Uri、byte[] 和许多其他来源一起使用。正如@Ajji 提到的:
Glide.with(context)
.load(videoFilePath) // or URI/path
.into(imgView); //imageview to set thumbnail to
Run Code Online (Sandbox Code Playgroud)如果您只需要最有效的位图 - 使用ThumbnailUtils. 就我而言,它生成了一个大小为 294 912 字节的位图(使用 Nexus5X - 1280x720 相机拍摄的视频),质量与下一种方法相同。用 90 压缩成 JPEG 后,它将生成一个 ~30Kb 的 jpeg 文件。
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path,
MediaStore.Images.Thumbnails.MINI_KIND);
Run Code Online (Sandbox Code Playgroud)最后一种方法是使用MediaMetadataRetriever. 但在我的例子中,它生成的位图大小比你得到的ThumbnailUtils(质量相同)大 6 倍以上。所以把它当作最后的手段。
MediaMetadataRetriever mMMR = new MediaMetadataRetriever();
mMMR.setDataSource(mContext, mAttachment.getUri());
bmp = mMMR.getFrameAtTime();
Run Code Online (Sandbox Code Playgroud)PS:不要忘记Bitmap,byte[]和real file .jpeg格式中的图像可以轻松地在这些类型中向任何方向转换。在 Uri 的情况下,您通常没有源文件的真实路径,但您始终可以像这样从中获取字节流:
InputStream in = mContext.getContentResolver().openInputStream(uri);
Run Code Online (Sandbox Code Playgroud)
有了这个输入流,你可以为所欲为。
您可以只使用FFmpegMediaMetadataRetriever而忘了反射:
/**
*
* @param path
* the path to the Video
* @return a thumbnail of the video or null if retrieving the thumbnail failed.
*/
public static Bitmap getVideoThumbnail(String path) {
Bitmap bitmap = null;
FFmpegMediaMetadataRetriever fmmr = new FFmpegMediaMetadataRetriever();
try {
fmmr.setDataSource(path);
final byte[] data = fmmr.getEmbeddedPicture();
if (data != null) {
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
}
if (bitmap == null) {
bitmap = fmmr.getFrameAtTime();
}
} catch (Exception e) {
bitmap = null;
} finally {
fmmr.release();
}
return bitmap;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47034 次 |
| 最近记录: |