Search code examples
androidandroid-activitylibgdx

How to take universal screenshot (View or GDX) on Android?


I created class which is getting screenshot of app each second. It takes screenshot in the next way:

View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

I works good for apps where activity is using RootView. But it is not working in situations where activities are creating in the next way:

public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
    cfg.useAccelerometer=false;
    cfg.useCompass=false;

    //init using new GdxGame() and cfg
}}

The problem is that there is no RootView so I can't get screenshot of game.

But my class need to be separate from app and it can't know if ther will be game or view and hot to take screenshot of game?


Solution

  • Don't use platform specific methods, you can do it by LibGDX.

    Taking a screenshot - LibGDX GitHub Wiki

    Remember that Gdx.files.external points to directory outside project files, but you can check it by getting value of Gdx.files.getExternalStoragePath().