Search code examples
androidmediaandroid-videoview

How to take the screen-shot of the current video frame in the videoView android?


I am using the video-view to play media and I want to take the screen shot of the current frame. I found some solutions which include using mediaMetaDataRetriever but that method is way too slow and does not perform well in many cases. I can not use media player over texture view to capture view I knew that approach, because I need by default media controls in my app. Is there any way to make that process fast? My CODE:

 public Bitmap getBitmapVideoView(){
        MediaMetadataRetriever mediaMetadataRetriever;
        mediaMetadataRetriever = new MediaMetadataRetriever();
        try {
            int currentPosition =videoView.getCurrentPosition();
            mediaMetadataRetriever.setDataSource(getVideoURL(),new HashMap<String, String>());
            Bitmap bitmap = mediaMetadataRetriever.getFrameAtTime(currentPosition*1000);
            if (bitmap != null) {
//                Canvas canvas = new Canvas(bitmap);
//                textureview.draw(canvas);
                return bitmap;
            }
            else{
                return null;
            }
        }
        catch (Exception e){
            Log.i("Errorrr",e.getMessage());
            return null;
        }
        finally {
            try {
                mediaMetadataRetriever.release();
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }

Approach:

Bitmap bitmap = Screenshot.INSTANCE.with((MainActivity)activity).setView(findViewById(R.id.relative)).setQuality(Quality.HIGH).getScreenshot();
                    LayoutInflater inflater = getLayoutInflater();
                    Toast toastShot = null;
                    View layout = inflater.inflate(R.layout.layout, (ViewGroup) findViewById(R.id.screen));
                    ImageView image = (ImageView) layout.findViewById(R.id.shot_image);
                    image.setImageBitmap(bitmap);
                    toastShot = new Toast(getApplicationContext());
                    toastShot.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                    toastShot.setDuration(Toast.LENGTH_LONG);
                    toastShot.setView(layout);
                    toastShot.show();

Solution

  • For taking screenshot you can use this library

    implementation 'com.github.MindorksOpenSource:screenshot:v0.0.1'
    

    And for taking screenshot just call this function on button click or any other event

     var b = Screenshot.with(activity!!).setView(rl_imageText).setQuality(Quality.HIGH).getScreenshot()
    

    Note: rl_imageText = This is the id of my xml relative layout of which i am taking screenshot.

    It will provide you the bitmap of the screenshot and for saving it into the storage or getting path use the below mentioned function

    private fun getImageUriFromBitmap(context: Context, bitmap: Bitmap): Uri {
        val bytes = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
        val path = MediaStore.Images.Media.insertImage(context.contentResolver, bitmap,"Title",null)
        return Uri.parse(path.toString())
    }
    

    Note: Please mention if it will not work so that i will provide you other solution.