VideoView getDrawingCache is returning black

2019-01-05 04:14发布

So I'm trying to do a screen shot of a VideoView. I figured the easiest way would be:

videoView.setDrawingCacheEnabled(true);

Then when I need to take a screenshot:

Bitmap screenshot = videoView.getDrawingCache();

But for some reason the bitmap I get back is just black every time. Anyone had any success with this? I also tried:

Bitmap bitmap = Bitmap.createBitmap(videoView.getWidth(), videoView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
videoView.draw(canvas);

But once again, this returns me a black image. I can see that the VideoView is hardly even documented in the Android javadocs. Any help?

2条回答
冷血范
2楼-- · 2019-01-05 04:38

Since API 10, you can use MediaMetadataRetriever to retrieve a frame at given time (in micro seconds). Here is a sample code:

public Bitmap videoFrame(String uri, long msec) {       
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {                       
        retriever.setDataSource(uri);            
        return retriever.getFrameAtTime(msec);
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    } catch (RuntimeException ex) {
        ex.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
        }
    }
    return null;
}
查看更多
SAY GOODBYE
3楼-- · 2019-01-05 04:46

From the docs for View#setDrawingCacheEnabled:

Enabling the drawing cache is similar to setting a layer when hardware acceleration is turned off. When hardware acceleration is turned on, enabling the drawing cache has no effect on rendering because the system uses a different mechanism for acceleration which ignores the flag. If you want to use a Bitmap for the view, even when hardware acceleration is enabled, see setLayerType(int, android.graphics.Paint) for information on how to enable software and hardware layers.

It's possible that the VideoView operates with hardware acceleration and is bypassing the caching mechanism. A good source dive might shed some light.

Edit - this post seems to clear some things up:

Sorry, by its nature a SurfaceView does not draw in the normal view hierarchy update system, so it won't be drawn in that.

(VideoView is a child of SurfaceView)

查看更多
登录 后发表回答