如何从服务器创建视频网址的缩略图
Posted
技术标签:
【中文标题】如何从服务器创建视频网址的缩略图【英文标题】:How to create thumbnail of video url form server 【发布时间】:2017-12-10 03:30:40 【问题描述】:如何创建url视频表单服务器的图片/缩略图
而我尝试不工作(不显示缩略图)
String String url = "http://clips.vorwaerts-gmbh.de/VfE_html5.mp4";
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(url,MediaStore.Images.Thumbnails.MINI_KIND);
Glide.with(context).load(thumb).asBitmap()
/*.override(convertDpToPx(100), convertDpToPx(100))*/
.placeholder(R.drawable.camera)
.override(200, 200)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new BitmapImageViewTarget(img_view)
protected void setResource(Bitmap resource)
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
//circularBitmapDrawable.setCircular(true);
img_view.setImageDrawable(circularBitmapDrawable);
);
我被 answer1 搞定了!
但它在缩略图方面遇到了一些问题,它的颜色变化很奇怪。
例子
【问题讨论】:
可能重复:***.com/questions/23522124/… Is it possible to Generate a thumbnail from a video url in android的可能重复 【参考方案1】:试试这个 像这样创建新的AsyncTask
public class DownloadImage extends AsyncTask<String, Void, Bitmap>
ImageView bmImage;
public DownloadImage(ImageView bmImage)
this.bmImage = (ImageView ) bmImage;
protected Bitmap doInBackground(String... urls)
Bitmap myBitmap = null;
MediaMetadataRetriever mMRetriever = null;
try
mMRetriever = new MediaMetadataRetriever();
if (Build.VERSION.SDK_INT >= 14)
mMRetriever.setDataSource(urls[0], new HashMap<String, String>());
else
mMRetriever.setDataSource(urls[0]);
myBitmap = mMRetriever.getFrameAtTime();
catch (Exception e)
e.printStackTrace();
finally
if (mMRetriever != null)
mMRetriever.release();
return myBitmap;
protected void onPostExecute(Bitmap result)
bmImage.setImageBitmap(result);
不如这样称呼AsyncTask
new DownloadImage(YourImageView).execute("Your URL");
编辑
或者您也可以使用Glide
从网址创建视频缩略图
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.placeholder_card_view);
requestOptions.error(R.drawable.placeholder_card_view);
Glide.with(getContext())
.load(path)
.apply(requestOptions)
.thumbnail(Glide.with(getContext()).load(path))
.into(ivVideoThumbnail);
【讨论】:
RequestOptions 未找到。 @NileshRathod DownloadImage 类中的变量“path”是什么,你还没有声明。 @KJEjava48 视频地址 @NileshRathod 我应该如何在这个后台任务中得到它。如果我使用变量“urls”我得到错误 @KJEjava48If i used variable "urls" i getting error
什么错误?【参考方案2】:
inner class LoadVideoThumbnail : AsyncTask<String, Any, Bitmap>()
override fun doInBackground(vararg objectURL: String): Bitmap
//return ThumbnailUtils.createVideoThumbnail(objectURL[0], Thumbnails.MINI_KIND);
val m = MediaMetadataRetriever()
m.setDataSource(objectURL[0], HashMap<String, String>())
var bm = m.getFrameAtTime(-1)
var decoded: Bitmap? = null
if (bm != null)
val stream = ByteArrayOutputStream()
bm.compress(Bitmap.CompressFormat.PNG, 20, stream)
decoded = BitmapFactory.decodeStream(ByteArrayInputStream(stream.toByteArray()))
return decoded!!
override fun onPostExecute(result: Bitmap)
//img.setImageBitmap(result);
Glide.with(this@EditItemsBaseActivity).
load(result)
.listener(object:RequestListener<Bitmap, GlideDrawable>
override fun onException(e: java.lang.Exception?, model: Bitmap?,
target: Target<GlideDrawable>?, isFirstResource: Boolean): Boolean
progresBar!!.visibility = View.GONE
return false
override fun onResourceReady(resource: GlideDrawable?, model: Bitmap?, target: Target<GlideDrawable>?, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean
progresBar!!.visibility = View.GONE
return false
).
into(videoImageView)
LoadVideoThumbnail().execute(videoUrl)
【讨论】:
以上是关于如何从服务器创建视频网址的缩略图的主要内容,如果未能解决你的问题,请参考以下文章