简体   繁体   中英

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.

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.

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.
  • 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.MINI_KIND which generates thumbnail of size 512 x 384.

Eg:

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

Here is the API docs for further info [Here] .

您可以使用Glide轻松实现:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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