How to generate Thumbnail of a Video for Android?

Asked

Viewed 764 times

8

I’m developing an app for android, one of the screens of the same should generate thumbnails videos and display them in a list. Like the image below. inserir a descrição da imagem aqui

I was able to generate thumbnails of images, but I tried several ways to generate thumbnails of 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 shown.

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 might be missing, or any other solution?

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

ps2: The screen looks like this when I test the app inserir a descrição da imagem aqui

  • Is there an error in the console or the Bitmap does not appear? My suggestion would be to use the Glide for two reasons: he can recover a thumbnail for videos and he does it asynchronously, which is the best way if he is planning to do it in an Adapter.

  • No error is shown on the console, the bitmap just doesn’t appear on the screen.

  • I tried to use the library with video and nothing is displayed, with images worked. : / ?

  • This was the result. https://www.dropbox.com/s/mhu1d8l7ymqri0i/Captura%20de%20tela%202016-02-19%2019.42.05.png? dl=0

2 answers

0

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

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 the same one. I never downloaded files through Genymotion, check the file path.

As you didn’t mention, I’ll ask. You checked the permissions in Manifest ?

0

Use the library Glide to generate 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);
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.