Is it possible to Generate a thumbnail from a video url in android
Asked Answered
S

9

32

I am working on a video app. I am streaming a video from server link , is it possible for me to generate a video thumbnail from the URL without downloading the video.

Srinagar answered 9/4, 2014 at 6:50 Comment(1)
is it a youtube video?Linetta
S
13

It is not possible to create thumbnail from steaming link, you have to show it from server. Better upload a thumbnail along the video. Use the below code to generate thumbnail

Bitmap bitmap = ThumbnailUtils.createVideoThumbnail("picturePath", MediaStore.Video.Thumbnails.MINI_KIND);
Srinagar answered 6/8, 2014 at 5:15 Comment(1)
this method is deprecatedSeeseebeck
E
83

Without downloading video you can generate thumbnail from below code:

public static Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable
{
    Bitmap bitmap = null;
    MediaMetadataRetriever mediaMetadataRetriever = null;
    try
    {
        mediaMetadataRetriever = new MediaMetadataRetriever();
        if (Build.VERSION.SDK_INT >= 14)
            mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
        else
            mediaMetadataRetriever.setDataSource(videoPath);
   //   mediaMetadataRetriever.setDataSource(videoPath);
        bitmap = mediaMetadataRetriever.getFrameAtTime();
    } catch (Exception e) {
        e.printStackTrace();
        throw new Throwable("Exception in retriveVideoFrameFromVideo(String videoPath)" + e.getMessage());

    } finally {
        if (mediaMetadataRetriever != null) {
            mediaMetadataRetriever.release();
        }
    }
    return bitmap;
}

NOTE : Video is stored as Intra and non Intra (Picture frames) getFrameAtTime will return the closest non- Intra frame as Bitmap. So basically it won't download the entire video.

Edomite answered 21/9, 2015 at 6:53 Comment(4)
If getFrameAtTime() downloads the entire video it defeats the purpose a little. I want to know if that method downloads the entire video. If so I would rather just download the video.Newsprint
@Newsprint Video is stored as Intra and non Intra (Picture frames) getFrameAtTime will return the closest non- Intra frame as Bitmap. So basically it won't download the entire video. Hope you got your answer.Edomite
@Edomite the String videoPath is the URL of the video?Liking
@ken: Sorry for late reply. But yes it is the URL of the video.Edomite
W
15

I tried this with glide and it worked , Glide version 4.3.1

    GlideApp.with(context)
                .asBitmap()
                .load(FILE_URL)
                .diskCacheStrategy(DiskCacheStrategy.DATA)
                .into(iv_picture);

Edit : Glide was working slow for me

The Top answer was not giving result for some videos , here is how i did it

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
 //give YourVideoUrl below
retriever.setDataSource("YourVideoUrl", new HashMap<String, String>());
// this gets frame at 2nd second
Bitmap image = retriever.getFrameAtTime(2000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC); 
//use this bitmap image
Wunder answered 15/12, 2017 at 11:16 Comment(1)
I think it is needed to release MediaMetadataRetriver after done. -> retriever.release()Angularity
S
13

It is not possible to create thumbnail from steaming link, you have to show it from server. Better upload a thumbnail along the video. Use the below code to generate thumbnail

Bitmap bitmap = ThumbnailUtils.createVideoThumbnail("picturePath", MediaStore.Video.Thumbnails.MINI_KIND);
Srinagar answered 6/8, 2014 at 5:15 Comment(1)
this method is deprecatedSeeseebeck
C
5

Here's your link:

  1. Android: Is it possible to display video thumbnails?
  2. http://developer.android.com/reference/android/media/ThumbnailUtils.html

In my opinion, Server side should create thumbnail from a video and transfer thumbnail video images through your service.

Cf answered 9/4, 2014 at 9:3 Comment(1)
hi i am able to create the thumbnail from the recorded video.But while streaming is it possible ?Srinagar
O
4

You can generate Thumbnail from server URL like given below

RequestOptions requestOptions = new RequestOptions();
requestOptions.isMemoryCacheable();
requestOptions.override(70,70);             
Glide.with(context).setDefaultRequestOptions(requestOptions).load(model.getMediaURL()).into(myViewHolder.imageView);
Omniumgatherum answered 2/1, 2020 at 5:57 Comment(0)
M
1
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail("VIDEO FILE ADDRESS", MediaStore.Video.Thumbnails.MINI_KIND);
Madelaine answered 29/12, 2017 at 11:51 Comment(1)
this method is deprecatedSeeseebeck
M
0

This method will give you thumbnails of all the video files in your phone... feel free to ask questions

public static Bitmap[] getThumbnail(Context context){
        Uri uri=MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] projection=new String[] { MediaStore.Video.Thumbnails._ID };
        Cursor ca = context.getContentResolver().query(uri, projection, null, null, null);
        Bitmap[] b=new Bitmap[ca.getCount()];
        int i=0;
        ca.moveToFirst();
        while(i<ca.getCount()) {
            int id = ca.getInt(ca.getColumnIndex(MediaStore.Video.Thumbnails._ID));
            b[i++]=MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),id, MediaStore.Images.Thumbnails.MINI_KIND, null );
            ca.moveToNext();
        }
        ca.close();
        return b;
    }
Madelaine answered 4/3, 2018 at 2:32 Comment(0)
A
0

Here is the best solution

"https://img.youtube.com/vi/" + videoID + "/0.jpg"

Here videoID you get from youtube Url.

Assertion answered 5/5, 2021 at 12:1 Comment(0)
P
-2

We fetch all video in Android Phone. http://sunilkmrnishad.blogspot.in/2017/03/read-files-apps-photos-media-from.html

public class ThumbnailExtract extends AsyncTask<String, long[], Bitmap> {

    private final String videoUrl;
    private final ImageView mThumbnail;
    private final boolean mIsVideo;
    private MediaMetadataRetriever mmr;

    public ThumbnailExtract(String videoLocalUrl, ImageView thumbnail, boolean isVideo) {
        this.videoUrl = videoLocalUrl;
        mThumbnail = thumbnail;
        mIsVideo = isVideo;
        if (!isVideo) {
            mmr = new MediaMetadataRetriever();
        }
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        if (!mIsVideo) {
            return getBitmap(videoUrl);
        } else {
            return ThumbnailUtils.createVideoThumbnail(videoUrl,
                    MediaStore.Images.Thumbnails.MINI_KIND);
        }
    }

    @Override
    protected void onPostExecute(Bitmap thumb) {
        if (thumb != null) {
            mThumbnail.setImageBitmap(thumb);
        }
    }

    private Bitmap getBitmap(String fileUrl) {
        mmr.setDataSource(fileUrl);
        byte[] data = mmr.getEmbeddedPicture();
        Bitmap bitmap = null;
        // convert the byte array to a bitmap
        if (data != null) {
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

        }
        return bitmap != null ? ScalingUtilities.createScaledBitmap(bitmap, 40, 40, ScalingUtilities.ScalingLogic.FIT) : bitmap;
    }
}
Pace answered 28/3, 2017 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.