Important: Please read the Qt Code of Conduct -

How to get screenshot in Java code on Android?

  • I need to make a screenshot in native Java code executed from QAndroidJniObject calls. The screenshot is to be stored inside instance.

    To accomplish this I get the view by this code


    QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative",
    "activity", "()Landroid/app/Activity;");
    QAndroidJniObject frameLayout = activity.callObjectMethod("findViewById","(I)Landroid/view/View;",
    0x01020002); // Hard coded value of

    Then pass the "frameLayout" value to this Java method:
        public static Bitmap loadBitmapFromView(View view) {
            View screenView = view.getRootView();
            Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
            return bitmap;
    But this does not work. I get only a big transparent rectangle on the Bitmap.
    What is the right way of taking screenshots from the Java code on Qt applications (QWidget without Qml)
    (It is quite important to not use QPixmap/QByteArray etc. and generate screenshot inside Java code)

Log in to reply