Thursday, May 5, 2011

Create thumbnail for a video using ThumbnailUtils

To create thumbnail for a video, the class android.media.ThumbnailUtils can be used.

Create thumbnail for a video using ThumbnailUtils

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/thumbnail_micro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/thumbnail_mini"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>


Java Code
package com.exercise.AndroidThumbnailUtils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.provider.MediaStore.Video.Thumbnails;
import android.widget.ImageView;

public class AndroidThumbnailUtils extends Activity {

String filePath = "/sdcard/Video/Royal Wedding.mp4";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView thumbnail_mini = (ImageView)findViewById(R.id.thumbnail_mini);
ImageView thumbnail_micro = (ImageView)findViewById(R.id.thumbnail_micro);

Bitmap bmThumbnail;

// MICRO_KIND: 96 x 96 thumbnail
bmThumbnail = ThumbnailUtils.createVideoThumbnail(filePath,
Thumbnails.MICRO_KIND);
thumbnail_micro.setImageBitmap(bmThumbnail);

// MINI_KIND: 512 x 384 thumbnail
bmThumbnail = ThumbnailUtils.createVideoThumbnail(filePath,
Thumbnails.MINI_KIND);
thumbnail_mini.setImageBitmap(bmThumbnail);
}
}


Download the files.


Related article:
- ThumbnailUtils
- Display Video thumbnail in ListView

2 comments:

  1. FileNotFoundExceptionOctober 31, 2013 at 9:21 AM

    Nice code, thank you.

    ReplyDelete
  2. How to play this video in next screen onClick on thumbnail.
    imageThumbnail.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(video, "video/mp4/3gp");
    startActivity(intent);
    //myVideoView.start();
    }
    });

    This is not working..I want to create a page dynamically which will open to play video in full screen and close automatically after finishing.

    ReplyDelete