Kalai Arasi
Kalai Arasi

Reputation: 249

Get Video Thumbnail and Display in ListView(To Android Genius Alone)

I currently have a method to fetch all the videos from the device and display in a listview. Currently I only show the album name and title with a default image for the thumbnail which repeats in all the rows of the listview. The method to do that is

    try {
        mAdapter = new SimpleCursorAdapter(
                this,
                // Use a template that displays a text view
                R.layout.media_select_row,
                null,
                // Map from database columns...
                new String[] {
                    MediaStore.Video.Media.ALBUM,
                    MediaStore.Video.Media.TITLE,
                    MediaStore.Video.Media._ID},
                    // To widget ids in the row layout...
                new int[] {
                    R.id.row_album,
                    R.id.row_title,
                    R.id.row_icon,},
                0);

        setListAdapter(mAdapter);

        getListView().setItemsCanFocus(true);

        // Normal click - open the editor
        getListView().setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent,
                    View view,
                    int position,
                    long id) {

                Cursor c = mAdapter.getCursor();
                int dataIndex = c.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                String filename = c.getString(dataIndex);

                videoFilename = filename;

                String substr = videoFilename.substring(videoFilename.length() - 3);

                if(substr.equals("mp4")) {
                    if(callPath == 1) {
                        Intent intent = new Intent(DubVideoList.this, DubStudio.class);
                        intent.putExtra("videoPath", videoFilename);
                        startActivity(intent);
                    }else if(callPath == 2){
                        Intent intent = new Intent(DubVideoList.this, DubStudioBeta.class);
                        intent.putExtra("videoPath", videoFilename);
                        startActivity(intent);
                    }
                }else{
                    Toast.makeText(DubVideoList.this,getString(R.string.mp4_support),Toast.LENGTH_LONG).show();
                }
            }
        });

        mInternalCursor = null;
        mExternalCursor = null;
        getLoaderManager().initLoader(INTERNAL_CURSOR_ID, null, this);
        getLoaderManager().initLoader(EXTERNAL_CURSOR_ID, null, this);

    } catch (SecurityException e) {
        // No permission to retrieve video?
        Log.e("SecurityError", e.toString());

        // TODO error 1
    } catch (IllegalArgumentException e) {
        // No permission to retrieve video?
        Log.e("IllegalArgument", e.toString());

        // TODO error 2
    }

    mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
          if (view.getId() == R.id.row_icon) {
                setSoundIconFromCursor((ImageView) view, cursor);
                return true;
            }

            return false;
        }
    });

Now I need to replace the default image for each cell with the Video's Thumbnail ? Is there a way to do that ? I have given the row's XML also for your reference. Thanks in advance.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

  <ImageView android:id="@+id/row_icon"
     android:gravity="center_vertical"
     android:layout_width="40dp"
     android:layout_height="40dp"
     android:layout_marginLeft="4dp"
      android:layout_marginTop="4dp"
      android:src="@drawable/filmicon"
      android:layout_gravity="center" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dip"
        android:layout_marginBottom="8dip"
        android:layout_width="0px"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="4dip">

        <TextView android:id="@+id/row_title"
            android:textColor="#ff000000"
            android:textSize="18sp"
            android:singleLine="true"
            android:shadowColor="#999999"
            android:shadowDx="1"
            android:shadowDy="1"
            android:shadowRadius="1"
            android:layout_width="wrap_content"
            android:layout_height="0dip"
            android:layout_weight="1"/>

        <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content">

            <TextView android:id="@+id/row_album"
            android:textColor="#ff1bbcff"
            android:textSize="12sp"
            android:singleLine="true"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

       </LinearLayout>

    </LinearLayout>

</LinearLayout>

Upvotes: 1

Views: 3004

Answers (2)

DILSHAD AHMAD
DILSHAD AHMAD

Reputation: 225

Use this code to add video thumb in listview.

Method :1

Bitmap bMap= ThumbnailUtils.createVideoThumbnail("local file path" , MediaStore.Video.Thumbnails.MINI_KIND); MViewHolder.imageThumb.setImageBitmap(bMap);

Method :2

Use Glide

compile 'com.github.bumptech.glide:glide:3.7.0'

Glide
.with( context ) .load( Uri.fromFile( new File( filePath ) ) ) .into( MViewHolder.imageThumb );

Upvotes: 0

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

To get the thumbnail, you can use this code

Bitmap thumb = ThumbnailUtils.createVideoThumbnail(video_path,
MediaStore.Images.Thumbnails.MINI_KIND);

Upvotes: 1

Related Questions