是否可以从android中的视频网址生成缩略图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22954894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to Generate a thumbnail from a video url in android
提问by DKV
I am working on a video app. I am streaming a video from server link , is it possible for me to generate a video thumbnail from the URL without downloading the video.
我正在开发一个视频应用程序。我正在从服务器链接流式传输视频,是否可以在不下载视频的情况下从 URL 生成视频缩略图。
采纳答案by DKV
It is not possible to create thumbnail from steaming link, you have to show it from server. Better upload a thumbnail along the video. Use the below code to generate thumbnail
无法从蒸汽链接创建缩略图,您必须从服务器显示它。最好沿视频上传缩略图。使用以下代码生成缩略图
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail("picturePath", MediaStore.Video.Thumbnails.MINI_KIND);
回答by KishuDroid
Without downloading video you can generate thumbnail from below code:
无需下载视频,您可以从以下代码生成缩略图:
public static Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable
{
Bitmap bitmap = null;
MediaMetadataRetriever mediaMetadataRetriever = null;
try
{
mediaMetadataRetriever = new MediaMetadataRetriever();
if (Build.VERSION.SDK_INT >= 14)
mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
else
mediaMetadataRetriever.setDataSource(videoPath);
// mediaMetadataRetriever.setDataSource(videoPath);
bitmap = mediaMetadataRetriever.getFrameAtTime();
} catch (Exception e) {
e.printStackTrace();
throw new Throwable("Exception in retriveVideoFrameFromVideo(String videoPath)" + e.getMessage());
} finally {
if (mediaMetadataRetriever != null) {
mediaMetadataRetriever.release();
}
}
return bitmap;
}
NOTE :Video is stored as Intra and non Intra (Picture frames) getFrameAtTime will return the closest non- Intra frame as Bitmap. So basically it won't download the entire video.
注意:视频存储为内部和非内部(图片帧)getFrameAtTime 将返回最接近的非内部帧作为位图。所以基本上它不会下载整个视频。
回答by Manohar Reddy
I tried this with glide and it worked , Glide version 4.3.1
我用滑翔试过这个,它奏效了,滑翔版 4.3.1
GlideApp.with(context)
.asBitmap()
.load(FILE_URL)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.into(iv_picture);
Edit : Glide was working slow for me
编辑:Glide 对我来说工作缓慢
The Top answer was not giving result for some videos , here is how i did it
最佳答案没有给出某些视频的结果,这是我是怎么做的
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//give YourVideoUrl below
retriever.setDataSource("YourVideoUrl", new HashMap<String, String>());
// this gets frame at 2nd second
Bitmap image = retriever.getFrameAtTime(2000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
//use this bitmap image
回答by koyot3
Here's your link:
这是你的链接:
- Android: Is it possible to display video thumbnails?
- http://developer.android.com/reference/android/media/ThumbnailUtils.html
In my opinion, Server side should create thumbnail from a video and transfer thumbnail video images through your service.
在我看来,服务器端应该从视频创建缩略图并通过您的服务传输缩略图视频图像。
回答by lost veteran
try using this one
尝试使用这个
Bitmap bitmap=ThumbnailUtils.createVideoThumbnail("VIDEO FILE
ADDRESS",MediaStore.Video.Thumbnails.MINI_KIND);
Bitmap bitmap=ThumbnailUtils.createVideoThumbnail("VIDEO FILE
ADDRESS",MediaStore.Video.Thumbnails.MINI_KIND);
回答by lost veteran
This method will give you thumbnails of all the video files in your phone... feel free to ask questions
此方法将为您提供手机中所有视频文件的缩略图...请随时提问
public static Bitmap[] getThumbnail(Context context){
Uri uri=MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection=new String[] { MediaStore.Video.Thumbnails._ID };
Cursor ca = context.getContentResolver().query(uri, projection, null, null, null);
Bitmap[] b=new Bitmap[ca.getCount()];
int i=0;
ca.moveToFirst();
while(i<ca.getCount()) {
int id = ca.getInt(ca.getColumnIndex(MediaStore.Video.Thumbnails._ID));
b[i++]=MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),id, MediaStore.Images.Thumbnails.MINI_KIND, null );
ca.moveToNext();
}
ca.close();
return b;
}
回答by Rahul Kamble
You can generate Thumbnail from server URL like given below
您可以从服务器 URL 生成缩略图,如下所示
- Add Glide Librarythen use like this following
- 添加Glide 库然后像下面这样使用
RequestOptions requestOptions = new RequestOptions(); requestOptions.isMemoryCacheable(); requestOptions.override(70,70); Glide.with(context).setDefaultRequestOptions(requestOptions).load(model.getMediaURL()).into(myViewHolder.imageView);
RequestOptions requestOptions = new RequestOptions(); requestOptions.isMemoryCacheable(); requestOptions.override(70,70); Glide.with(context).setDefaultRequestOptions(requestOptions).load(model.getMediaURL()).into(myViewHolder.imageView);
回答by Sunil
We fetch all video in Android Phone. http://sunilkmrnishad.blogspot.in/2017/03/read-files-apps-photos-media-from.html
我们获取 Android 手机中的所有视频。http://sunilkmrnishad.blogspot.in/2017/03/read-files-apps-photos-media-from.html
public class ThumbnailExtract extends AsyncTask<String, long[], Bitmap> {
private final String videoUrl;
private final ImageView mThumbnail;
private final boolean mIsVideo;
private MediaMetadataRetriever mmr;
public ThumbnailExtract(String videoLocalUrl, ImageView thumbnail, boolean isVideo) {
this.videoUrl = videoLocalUrl;
mThumbnail = thumbnail;
mIsVideo = isVideo;
if (!isVideo) {
mmr = new MediaMetadataRetriever();
}
}
@Override
protected Bitmap doInBackground(String... params) {
if (!mIsVideo) {
return getBitmap(videoUrl);
} else {
return ThumbnailUtils.createVideoThumbnail(videoUrl,
MediaStore.Images.Thumbnails.MINI_KIND);
}
}
@Override
protected void onPostExecute(Bitmap thumb) {
if (thumb != null) {
mThumbnail.setImageBitmap(thumb);
}
}
private Bitmap getBitmap(String fileUrl) {
mmr.setDataSource(fileUrl);
byte[] data = mmr.getEmbeddedPicture();
Bitmap bitmap = null;
// convert the byte array to a bitmap
if (data != null) {
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
}
return bitmap != null ? ScalingUtilities.createScaledBitmap(bitmap, 40, 40, ScalingUtilities.ScalingLogic.FIT) : bitmap;
}
}