Can not take texture view screenshot in android

387 Views Asked by At

I have a texture view on which a video is getting played. I need to take screenshot of the frame of playing video. Earlier I was using a method

public Bitmap getFrameAsBitmap() {
        Bitmap bmp = textureview.getBitmap();
        Canvas canvas = new Canvas(bmp);
        textureview.draw(canvas);
        return bmp;
    }

It is working fine on most of the devices but on Samsung M series I can not take screenshot. Only black screen is coming. Then I tried

public Bitmap getFrameAsBitmap() {
        View view = textureview;
        view.setDrawingCacheEnabled(true);
        Bitmap bmp = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        textureview.draw(canvas);
        return bmp;
    }

But this method is not returning data on any phone. Any ideas what to do?

1

There are 1 best solutions below

0
On

Here are several things that you can try.

Return the bitmap directly.

public Bitmap getFrameAsBitmap() {
    return textureview.getBitmap();
}

Clone the bitmap then return the clone.

public Bitmap getFrameAsBitmap() {
    Bitmap bmp = textureview.getBitmap();
    Bitmap clone = bmp.copy(Bitmap.Config.ARGB_8888, true);
    return clone;
}

Draw the bitmap on a new canvas.

public Bitmap getFrameAsBitmap() {
    Bitmap bmp = textureview.getBitmap();
    //Try using bitmap's width/height first, if it does not work, use view's width/height instead.
    Bitmap newBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
    //Bitmap newBmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(newBmp);
    //It is not efficient to create a Paint object too often. Better make it as global variable.
    canvas.drawBitmap(bmp, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG));
    return newBmp;
}