Vladimir Djukic
Vladimir Djukic

Reputation: 965

Nativscript getRootView() not working

In my root component I am trying to take screenshoot like this:

private getImage(v) {
        if (topmost().android) {
            const view = android.activity.getWindow().getDecorView().getRootView();
            view.setDrawingCacheEnabled(true);
            const bmp = android.graphics.Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);

            var source = new imageSource.ImageSource();
            view.source.setNativeSource(bmp);
            return source;
        }
    }

But I getting error that I cannot call getWindow function on undefined...

JS: ERROR TypeError: Cannot read property 'getWindow' of undefined

Anyone know how to do this?

Upvotes: 1

Views: 601

Answers (1)

bhavin jalodara
bhavin jalodara

Reputation: 1530

use application.android.foregroundActivity or application.android.context. this will help you get reference to current activity. and then you can use

    import * as application from "application";
    const view = application.android.foregroundActivity.getWindow().getDecorView().getRootView();
    view.setDrawingCacheEnabled(true);
    const bmp = android.graphics.Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);

here is playground demo link if you need it:https://play.nativescript.org/?template=play-ng&id=cmvjgq

Upvotes: 1

Related Questions