How to generate Thumbnail of a video for Android?

I am developing an application for android, One of the screens of the same should generate thumbnails of videos and display them in a list. As the picture below. insert the description of the image here

I managed to generate thumbnails of images, but I have tried several ways to generate the thumbnails of the videos, and nothing is displayed on the screen when I test the application. My last attempt was to create only a thumbnail of a video, but as in other attempts nothing is available.

Currently the code is as follows below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="patricia.videothumbnail.MainActivity">


    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Titulo do Video"
        android:id="@+id/textView"
        android:layout_alignBottom="@+id/iv"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="20sp"
        android:gravity="center"
        android:background="@color/primary"
        android:textColor="@color/icons"
        />



</RelativeLayout>

Java

import android.app.Activity;
import android.provider.MediaStore.Video.Thumbnails;


public class MainActivity extends AppCompatActivity {


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


        //caminho para o video, testei de diversas formas
        //1ª tentativa              
        String filePath = "android.resource://" + getPackageName() + "/" + R.raw.destruction;

        //2º tentativa
        //String filePath = "/storage/external_SD/destruction.mp4";

        Bitmap bmThumbnail;

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

According to the Android documentation and several examples I found, this code should work. Does anyone have an idea of what may be missing, or some other solution?

Ps: for a suggestion I have already tried to use the Glide library, I continue with the same problem.

Ps2: the screen looks like this when you text the app insert the description of the image here

Author: Patrícia Ribeiro, 2016-02-18

2 answers

Well, first of all, never pass the file path that way. Do this:

File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"destruction.mp4");

Then, to actually take the path, use file.getAbsolutePath()

This will give you the right path in the right way.

Now, make sure that really the path you are going through is that same. Never accessed files through Genymotion, check the file path.

As you did not mention, I will ask. Have you checked the permissions in Manifest ?

 0
Author: Liberi, 2016-03-11 20:14:48

Use The Library Glide to generate the thumbnails, it works for both local files and online files, this is a real example of my application that uses this library:

    @Override
    public void onBindViewHolder(@NonNull GalleryHolder holder, int position) {

        GalleryItem item = mItems.get(position);

        holder.listener = mListener;

        if(!item.isFolder){

            DisplayMetrics dm = new DisplayMetrics();
            mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            holder.ivThumb.getLayoutParams().height = dm.heightPixels / 5;

            RequestOptions options = new RequestOptions().override(holder.ivThumb.getWidth(), dm.heightPixels / 5).centerCrop();
            Glide.with(mActivity).load(item.getFile()).apply(options).into(holder.ivThumb);

        }

    }

    ...

    class GalleryHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        GalleryListener listener;
        ImageView ivThumb;

        GalleryHolder(@NonNull View itemView) {
            super(itemView);

            ivThumb = (ImageView)itemView.findViewById(R.id.item_gallery_thumb);
            ivThumb.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            listener.onItemClick(getAdapterPosition(), false);
        }
    }
 0
Author: Samuel Ives, 2018-12-09 15:28:52