How to Generate Thumbnail from a Video for Android?

6

I'm developing an android application, one of the screens of it should generate thumbnails of videos and display them in a list. Like the image below.

Iwasabletogeneratethumbnailsofimages,butI'vetriedseveralwaystogeneratethethumbnailsofthevideos,andnothingisdisplayedonthescreenwhenItesttheapplication.Mylastattemptwastocreateonlyonethumbnailofavideo,butasintheotherattemptsnothingisdisplayed.

Currentlythecodeisasfollows:

<?xmlversion="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 documentation of Android and several examples that I found, this code should work. Does anyone have an idea of what might be missing, or some other solution?

ps: For a suggestion I've already tried using the Glide library, I still have the same problem.

ps2: The screen looks like this when I test the app

    
asked by anonymous 18.02.2016 / 19:20

2 answers

0

Well, first, never pass the file path this way. Here's how:

File sdcard = Environment.getExternalStorageDirectory();

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

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

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

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

As you did not mention, I'll ask. Have you checked the permissions on Manifest?

    
11.03.2016 / 21:14
0

Use the Glide library 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);
        }
    }
    
09.12.2018 / 16:28