3

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.

tobyUCT
  • 137
  • 9

1 Answers1

0

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

Karthik
  • 4,950
  • 6
  • 35
  • 65
  • It differs from try to try, but I never get the result I want. On the last try I had the same screenshot everytime i touched down (I also draw a circle on the position of the touch down event in the lower half of the screen). The part of the screenshot where the webview should be is now white all the time. Another question: should I enable/disable the drawingCache before and after each screenshot? – tobyUCT Jul 02 '15 at 13:25
  • you might want to try (http://developer.android.com/reference/android/webkit/WebView.html#saveWebArchive(java.lang.String)) If the cache is disabled `getDrawingCache()` will return null. So it's a safer option to enable cache. – Karthik Jul 02 '15 at 14:00
  • Thanks for the fast replies :). I want to access the screenshots on my PC later on. If I understand correctly saveWebArchive does not allow me to do that. At least I did not manage to save it to the folder I saved the images before. (that path was android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString()+ "/saved_files/images/" and then I added "filename.jpg" for images (filename being time of day at the moment of touchdown) for saveWebArchive i changed the ending to .xml – tobyUCT Jul 02 '15 at 14:31
  • I have updated the answer, check it and let me know If that is what you were trying to achieve. – Karthik Jul 02 '15 at 15:22
  • Unfortunately it hasn't worked yet. I edited some code to my question. I have never used ValueCallback before. A breakpoint at the onReceiveValue function tells me "Cannot find local variable 'value'). – tobyUCT Jul 03 '15 at 08:19
  • that's strange http://developer.android.com/reference/android/webkit/ValueCallback.html clearly says that `onRecieveValue() is Invoked when the value is available.` – Karthik Jul 03 '15 at 09:25