简体   繁体   English

Android-视频文件中的缩略图

[英]Android - Thumbnail from video file

I currently have a app where the user click on the floating action button and selects a video file, the file is then saved to a different folder. 我目前有一个应用程序,用户单击浮动操作按钮并选择一个视频文件,然后将该文件保存到另一个文件夹中。 I want to then show thumbnails of all the video's. 然后,我想显示所有视频的缩略图。 I've seen a tutorial series where it is done with MediaStore, but then I can't set the path of the uri. 我看过一个教程系列,该教程使用MediaStore完成,但是后来我无法设置uri的路径。

Can someone please point me in the right direction? 有人可以指出正确的方向吗?

Here is my class to open the gallery and save the video to a different path: 这是我的课程,用于打开图库并将视频保存到其他路径:

public class Activity extends AppCompatActivity {
    private static final int pick = 100;
    Uri videoUri;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            openGallery();
            }
        });
    }

    private void openGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, pick);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == pick) {
            try
            {
                Log.e("videopath","videopath");
                AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
                FileInputStream fis = videoAsset.createInputStream();
                File root=new File(Environment.getExternalStorageDirectory(),"MYFOLDER");

                if (!root.exists()) {
                    root.mkdirs();
                }

                File file;
                file=new File(root,"android_"+System.currentTimeMillis()+".mp4" );

                FileOutputStream fos = new FileOutputStream(file);

                byte[] buf = new byte[1024];
                int len;
                while ((len = fis.read(buf)) > 0) {
                    fos.write(buf, 0, len);
                }
                fis.close();
                fos.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            videoUri = data.getData();
        }
    }
  }

You can use the ThumbnailUtils class in Android for this purpose. 为此,您可以使用Android中的ThumbnailUtils类。

public static Bitmap createVideoThumbnail (String filePath, int kind)

this method returns a bitmap of the video. 此方法返回视频的位图。

  • First parameter is the filepath, ie the location of the video file to be passed as String. 第一个参数是文件路径,即要作为String传递的视频文件的位置。
  • Second parameter is the kind of bitmap that you need, there are two types: 第二个参数是所需的位图类型,有两种类型:

    • MediaStore.Images.Thumbnails.MICRO_KIND which generates thumbnail of size 96 x 96 MediaStore.Images.Thumbnails.MICRO_KIND生成大小为96 x 96的缩略图
    • MediaStore.Images.Thumbnails.MINI_KIND which generates thumbnail of size 512 x 384. MediaStore.Images.Thumbnails.MINI_KIND生成大小为512 x 384的缩略图。

Eg: 例如:

Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MINI_KIND);

Here is the API docs for further info [Here] . 这是API文档,以获取更多信息[此处]

您可以使用Glide轻松实现:

Glide.with(context).load(videoPath).asBitmap().into(imageView);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM