Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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 android.graphics.Bitmap instance.

    To accomplish this I get the android.R.id.content 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 android.R.id.content

    Then pass the "frameLayout" value to this Java method:
    
        public static Bitmap loadBitmapFromView(View view) {
            View screenView = view.getRootView();
            screenView.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
            screenView.setDrawingCacheEnabled(false);
            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