2017-12-18 80 views
-3

需要从存储在服务器中的视频生成缩略图,下面是我的代码,但“路径”变量给出问题,如何解决问题。如果我删除路径参数与URL参数,然后我得到了2级或3的视频生成相同的缩略图的缩略图,但没有按照正确的顺序或有时,下面是我的代码 -来自服务器的视频的缩略图未在ImageView中显示

Video video = mVideos.get(position); 
      //play video using android api, when video view is clicked. 
      url = video.getVideoUrl(); // your URL here 
      Uri videoUri = Uri.parse(url); 

      new DownloadImage(holder.videothumbView).execute(url); 




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(path, new HashMap<String, String>()); 
       else 
        mMRetriever.setDataSource(path); 
       myBitmap = mMRetriever.getFrameAtTime(); 
      } catch (Exception e) { 
       e.printStackTrace(); 


      } finally { 
       if (mMRetriever != null) { 
        mMRetriever.release(); 
       } 
      } 
      return myBitmap; 
     } 

     protected void onPostExecute(Bitmap result) { 
      bmImage.setImageBitmap(result); 
     } 
    } 
+2

的可能的复制[如何创建视频的URL格式服务器的缩略图(https://stackoverflow.com/questions/ 44943575 /如何创建thumbnail-of-video-url-form-server) –

+2

可能有[如何显示视频缩略图? Android](https://stackoverflow.com/questions/31646361/how-to-display-a-thumbnail-for-a-video-android) – vinS

+0

**不推荐使用BitmapDrawable **,像这样使用它'BitmapDrawable bitmapDrawable = BitmapDrawable(context.getResources(),thumb);' –

回答

-1

所有与网络相关的任务必须是在一个单独的线程中完成。你不能在主线程中完成它。您可以使用Picasso图像库。它是开源的,你可以显示像装载不同的国家不同的图像,差错等

Picasso.with(context) // your activity of other context referance 
.load(video.getVideoUrl()) 
.placeholder(R.drawable.user_placeholder) // this will show during image loading from network 
.error(R.drawable.user_placeholder_error) // error image to display 
.into(holder.videothumbView);