Take a screenshot of a video in a webview android

萝らか妹 提交于 2020-01-04 00:19:21

问题


I am working on a project where I save a screenshot in a WebView. API Level is 21. On a normal homepage it works absolutely fine, but when I visit youtube and watch a video, the video player returns as a black rectangle. What I want to do is take a screenshot whenever the user touches the screen.

When I take a screenshot with Power & Volume Button it works perfectly. Is there an Intent which takes this kind of a screenshot? Ideally the screenshot would only contain the webview, but if it's the entire screen I can work with that too.

Any help is greatly appreciated.

Cheers,

Toby

Edit: some of my code

View rootView;
ValueCallback<String> callback = new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String value) {
        category1.setText(value);
    }
};
String root = android.os.Environment.getExternalStoragePublicDirectory
        (Environment.DIRECTORY_DCIM).toString(), filename, time, puretime, movementType = ""; //movementType is set according to MotionEvent.getAction()
File myDir = new File(root + "/saved_files/")

protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
}

public class MyView extends View {
    public MyView(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        calendar = Calendar.getInstance();
        time = String.format("%02d:%02d:%02d.%03d", calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND),
                calendar.get(Calendar.MILLISECOND));
        puretime = String.format("%02d%02d%02d%03d", calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND),
                calendar.get(Calendar.MILLISECOND));
        if (movementType.equals("down")) {
            try {
                filename = puretime + ".xml";
                webView.saveWebArchive(myDir+filename, false, callback);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        invalidate();
        return true;
    }

edit2:

apparently the problem for the black rectangles is that elements as the video are processed by the gpu and for a "normal" snapshot to work the program would need to calculate every frame which is too much work.


回答1:


I think this will help

  View rootView = findViewById(android.R.id.content).getRootView();
  rootView.setDrawingCacheEnabled(true);
  Bitmap screenshot = rootView.getDrawingCache();

Let me know once you try.

UPDATE:

If I have understood your comment correctly, you were not able to change path with saveWebArchive(filename).

Then you might want to use saveWebArchive(String,boolean,ValueCallback<String> callBack). This will save file with the given name and you can use that file name in the callback function to do whatever you want to (like saving to external storage or anything).

Check here - saveWebArchive



来源:https://stackoverflow.com/questions/31182503/take-a-screenshot-of-a-video-in-a-webview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!