Vimeo Video Thumbnail in an ImageView

问题: I am developing an Android app which should display a list of video thumbnails in RecyclerView. A new activity will then play the video for selected thumbnail. How do I set...

问题:

I am developing an Android app which should display a list of video thumbnails in RecyclerView. A new activity will then play the video for selected thumbnail. How do I set com.vimeo.networking.model.Picture to Android ImageView?

My code:

mVimeoClient.fetchNetworkContent(VIDEO_URI, new ModelCallback<VideoList>(VideoList.class) {
           @Override
           public void success(VideoList videoList) {
                ArrayList<Video> videoArrayList = videoList.data;
               Video video = videoArrayList.get(0);
               ArrayList<com.vimeo.networking.model.Picture> arrayListPics = video.pictures.sizes;
               // imageView.setImageDrawable((Drawable) pictureAL.get(0));
               imageView.setImageBitmap( (Bitmap) arrayListPics.get(0));
           }
           @Override
           public void failure(VimeoError error) {
           }
        });
}

The setImageBitmap() And setImageDrawable() throws

java.lang.ClassCastException

回答1:

Please user Picasso library to load image in Image-view.

In gradle

implementation 'com.squareup.picasso:picasso:2.71828'

in your adapter class

mVimeoClient.fetchNetworkContent(VIDEO_URI, new ModelCallback<VideoList>(VideoList.class) {
           @Override
           public void success(VideoList videoList) {
                ArrayList<Video> videoArrayList = videoList.data;
               Video video = videoArrayList.get(0);
               ArrayList<Pictures> arrayListPics = video.pictures.sizes;


Picasso.get().load(arrayListPics.get(0).getpath).into(view);
           }
           @Override
           public void failure(VimeoError error) {
           }
        });
}

回答2:

You are trying to cast Pictures to Bitmap

This arrayListPics.get(0) returns a Pictures object. Depending on the content of the Pictures class you'll have to create a Bitmap Object.

Then use it in your imageView.setImageBitmap (your_bitmap_oject_here);

Edit: Ok, I saw from your edit that by Pictures you were referring to com.vimeo.networking.model.Picture. In this case you'll have to download the image using the uri attribute then load it into your ImageView.


回答3:

The Picture object (the one returned from arrayListPics.get(0)) in the vimeo-networking library isn't a Bitmap and therefore can't be cast to one. The Picture object has a field on it called mLink which can be access via Picture#getLink(). This will return a URI string which you then can set on your ImageView.

The simplest code you could use to get this working is:

    // The ImageView you want to put the thumbnail in
    ImageView yourImageView = <insert your ImageView here>;
    // The video whose thumbnail you want
    Video yourVideo = <insert your Video here>;
    // The collection of `Picture` objects associated with this video.
    // Each `Picture` in this "collection" is a different size
    PictureCollection yourVideosPictures = yourVideo.getPictures();
    // Get the first thumbnail/picture from this collection (not recommended)
    Picture videoThumbnailPicture = yourVideosPictures.getPictures().get(0);
    // The URI to the image of the thumbnail
    String videoThumbnailUri = videoThumbnailPicture.getLink();
    // Convert the String URI to an actual URI object
    final Uri uri = Uri.parse(videoThumbnailUri);
    yourImageView.setImageURI(uri);

I say this is the simplest because there are more things you should do when setting an image uri. One thing is you should base the Picture your grab from yourVideosPictures based on the width of your ImageView so that you're not needlessly pulling down a larger image than you need.

You should also probably not just set the image URI directly onto yourImageView, but instead you should use some image caching library (or some caching implementation).

I'd suggest looking into Picasso, Glide, or Fresco. Or just google "Image caching on Android".

  • 发表于 2018-07-06 03:29
  • 阅读 ( 764 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除