Android Convert current screen to bitmap
Asked Answered
S

4

7

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.

Submit answered 25/4, 2014 at 13:24 Comment(2)
So a screenshot? code.google.com/p/android-screenshot-librarySpeller
you see, I don't want the user to know that a screenshot has been taken. Just capture the screen load it into a bitmap and then set the bitmap to an image viewSubmit
D
10

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;
    }
Disseisin answered 25/4, 2014 at 13:30 Comment(3)
ok, what does get drawing cache do? I want to capture the screen not just a single view. My screen is fragmented, there are many views that are distributed across xml layout files, merged together in one viewSubmit
If you pass the layout root or main, it takes a snapshot of what's on the screen ;)Disseisin
What does get drawing cache do? You need read this getDrawingCacheDisseisin
F
0

View is you root layout:-

View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, getString(R.string.free_tiket)+".jpg");
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(myPath);
    b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
    MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

see link for more info :-

http://code.google.com/p/android-screenshot-library/wiki/DeveloperGuide

Android - How to take screenshot programatically

Federalist answered 25/4, 2014 at 13:29 Comment(1)
perhaps you didn't read my above comment. I don't want to actually take a screenshot. Just take the screen convert it into bitmap and set it as the ImageBitmap of a ImageView.Submit
R
0
View rootView = findViewById(R.id.relative_facebook).getRootView();
        rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();
Renvoi answered 25/4, 2014 at 13:38 Comment(0)
L
0

In 2023, it seems PixelCopy is suggested, and getDrawingCache is deprecated.

More details: When reading getDrawingCache as is suggested by other answers, Android says:

This method was deprecated in API level 28. The view drawing cache was largely made obsolete with the introduction of hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache layers are largely unnecessary and can easily result in a net loss in performance due to the cost of creating and updating the layer. In the rare cases where caching layers are useful, such as for alpha animations, setLayerType(int, android.graphics.Paint) handles this with hardware rendering. For software-rendered snapshots of a small part of the View hierarchy or individual Views it is recommended to create a Canvas from either a Bitmap or Picture and call draw(android.graphics.Canvas) on the View. However these software-rendered usages are discouraged and have compatibility issues with hardware-only rendering features such as Config.HARDWARE bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback reports or unit testing the PixelCopy API is recommended.

As can be seen from the text I marked bold, PixelCopy is suggested.

Londalondon answered 21/6, 2023 at 3:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.