如何获取 /sdcard/Android/data/mypackage/files 文件夹中视频的缩略图?
对 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();
Query to MediaStore.Video.Media.EXTERNAL_CONTENT_URI
returns only video in /sdcard/DCIM/100MEDIA
But I want to get thumbnails for video in my /sdcard/Android/data/mypackage/files
folder. Is it possible ?
Here is part of my code:
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
如果您使用的是 android-8 (Froyo) 或更高版本,则可以使用 ThumbnailUtils.createVideoThumbnail:
If you are on android-8 (Froyo) or above, you can use ThumbnailUtils.createVideoThumbnail:
使用 Glide 它将异步获取缩略图。
Use Glide it will fetch the thumbnail in async.
从视频中获取缩略图的 3 种方法:
最好的方法是使用 Glide 。它将在后台完成所有工作,将缩略图直接加载到 ImageView 中,甚至可以在加载时显示动画。它可以与 Uri、byte[] 和许多其他源一起使用。
正如@Ajji 提到的:
如果您只需要以最有效的方式获取位图 - 使用
ThumbnailUtils
。就我而言,它生成了一个大小为 294 912 字节的位图(使用 Nexus5X 相机拍摄的视频 - 1280x720),质量与下一种方法相同。用 90 压缩成 JPEG 后,将生成约 30Kb 的 jpeg 文件。
最后一种方法是使用
MediaMetadataRetriever
。但就我而言,它生成的位图大小比使用ThumbnailUtils
生成的位图大 6 倍以上(具有相同的质量)。因此,请将其视为最后的手段。PS:不要忘记,
Bitmap
、byte[]
和real file .jpeg
格式的图像可以在这些格式中以任何方向轻松转换类型。对于 Uri,您通常没有源文件的真实路径,但您始终可以像这样从中获取字节流:并且使用此输入流您可以做任何您想做的事情。
3 ways to get a thumbnail from a video:
The best way is to use Glide. It will do all the work in the background, load the thumbnail right into the ImageView and even can show animation when loading. It can work with Uri, byte[] and many other sources.
As @Ajji mentioned:
If you just need a bitmap in the most efficient way - use
ThumbnailUtils
.In my case, it produced a bitmap with a size of 294 912 bytes (video taken with a camera of Nexus5X - 1280x720) and the quality was the same as in the next approach. After you compress into JPEG with 90 it will generate a jpeg file of ~30Kb.
The last approach is to use
MediaMetadataRetriever
. But in my case, it produced a bitmap with size more than 6 times bigger than you got withThumbnailUtils
(with the same quality). So consider it as a last resort.P.S.: Don't forget that images in
Bitmap
,byte[]
andreal file .jpeg
formats can be easily converted in any direction within these types. In case of Uri's you often don't have real path to the source file but you can always get the byte stream from it like this:and with this input stream you can do whatever you want.
您可以只使用 FFmpegMediaMetadataRetriever 并忘记反射:
You can just use FFmpegMediaMetadataRetriever and forget the reflection:
使用选项加载位图以减小位图大小。
Use Options to load bitmap of decrease the bitmap size..
请参阅@Ajji的回答:
它有时会返回黑色图像,这个问题已在 Glide 库的问题中提到
使用此代码:
see @Ajji 's answer :
It sometimes returns black image, this issue is already mentioned in Glide library's issues
Use this code:
从 VIDEO_ID 获取视频缩略图:
Get video thumbnail from VIDEO_ID:
您可以将此方法与任何
Uri
结合使用:You can use this method with any
Uri
:这是与马修·威利斯类似的答案,但增加了反思。为什么?因为科学。
Here is a similar answer to Matthew Willis but with added reflection. Why? because science.
如果您直接创建缩略图如下
使用 SuziLoader
此加载程序将在后台加载本地存储在文件系统上的视频缩略图。
第 1 步。将 JitPack 存储库添加到您的构建文件
将其添加到存储库末尾的根 build.gradle 中:
添加依赖项ADD READ EXTERNAL STORAGE Permission
第 2 步。在清单中
If you are directly creating thumbnails as follows
Use SuziLoader
This loader will load the thumbnails for the videos which is locally stored on your filesystem in background.
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
Step 2. Add the dependency
ADD READ EXTERNAL STORAGE Permission in manifest
尝试类似于此代码片段的操作:
Try something similar to this code snippet: