androidscreenandroid-imageviewandroid-bitmap

Android Convert current screen to bitmap


I would like to convert a screen of my app and convert it into a Bitmap. I already know how to convert a view into a bitmap, but that's not what I want as my screen is made up of many fragments. Thus I'd like to be able to take the screen and convert it into a bitmap... Help is highly appreciated thanks.


Solution

  • You could use something like this and pass the layout as a view ;)

    public Bitmap takeScreenShot(View view) {
            // configuramos para que la view almacene la cache en una imagen
            view.setDrawingCacheEnabled(true);
            view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
            view.buildDrawingCache();
    
            if(view.getDrawingCache() == null) return null; // Verificamos antes de que no sea null
    
            // utilizamos esa cache, para crear el bitmap que tendra la imagen de la view actual
            Bitmap snapshot = Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);
            view.destroyDrawingCache();
    
            return snapshot;
        }