来自服务器的视频缩略图未在ImageView中显示

时间:2017-12-18 09:51:37

标签: java android

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

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

1 个答案:

答案 0 :(得分:-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);