How to create thumbnail of video url form server

后端 未结 2 1467
孤街浪徒
孤街浪徒 2020-11-29 12:03

How to create image/thumbnails of url video form server

and I try is not work (not show thumbnails)

String  String url = \"http://clips.vorwaerts-gm         


        
相关标签:
2条回答
  • 2020-11-29 12:31
    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)

    0 讨论(0)
  • 2020-11-29 12:39

    Try this Create new AsyncTask like this

    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);
        }
    }
    

    Than call this AsyncTask like this

       new DownloadImage(YourImageView).execute("Your URL");
    

    EDIT

    Or you can also use Glide to create thumbnail of video from url

     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);
    
    0 讨论(0)
提交回复
热议问题