SoFunction
Updated on 2025-04-10

Android implementation converts View into pictures and saves them locally

This article shares the specific code for Android to convert View into pictures and save them to local. For your reference, the specific content is as follows

1. Overview

There are requirements in the app that need to convert the View into an image and save it locally. Here are two situations:

It has been displayed on the interface itself
Not added to the interface or not displayed (drawn)

2. Implementation method

For the first case above, I can use the following code:

private void viewSaveToImage(View view) {
        (true);
        (View.DRAWING_CACHE_QUALITY_HIGH);
        ();

        // Convert a View to an Image        Bitmap cachebmp = loadBitmapFromView(view);

        FileOutputStream fos;
        String imagePath = "";
        try {
            // Determine whether the mobile phone device has an SD card            boolean isHasSDCard = ().equals(
                    .MEDIA_MOUNTED);
            if (isHasSDCard) {
                // SD card root directory                File sdRoot = ();
                File file = new File(sdRoot, ().getTimeInMillis()+".png");
                fos = new FileOutputStream(file);
                imagePath = ();
            } else
                throw new Exception("File creation failed!");

            (, 90, fos);

            ();
            ();

        } catch (Exception e) {
            ();
        }
        ("imagePath="+imagePath);

        ();
    }

    private Bitmap loadBitmapFromView(View v) {
        int w = ();
        int h = ();

        Bitmap bmp = (w, h, .ARGB_8888);
        Canvas c = new Canvas(bmp);

        ();
        /** If the canvas canvas is not set to white, a transparent one is generated */

        (0, 0, w, h);
        (c);

        return bmp;
 }

If it is the second type and still used in this way, an error will be reported, because the View does not get the actual size before adding it to the container, so you need to specify the size of the View first:

DisplayMetrics metric = new DisplayMetrics();
                    getWindowManager().getDefaultDisplay().getMetrics(metric);
                    int width = ;     // Screen width (pixels)                    int height = ;   // Screen height (pixels)                    View mingpianView = (this).inflate(.view_team_mingpian, null, false);
                    layoutView(mingpianView, width, height);
//Then the View and its inner subView have actual sizes, that is, the layout is completed, which is quite added to the interface.  Then you can create a bitmap and draw it on it:    private void layoutView(View v, int width, int height) {
        // The size of the entire View is the coordinates of the upper left corner and the lower right corner        (0, 0, width, height);
        int measuredWidth = (width, );
        int measuredHeight = (10000, .AT_MOST);
        /** Of course, after the measurement is completed, the size of the View will not be actually changed, and the method needs to be called to layout.
 * After calling the layout function according to the example, the size of the View will become the size you want to set.
 */
        (measuredWidth, measuredHeight);
        (0, 0, (), ());
    }

Then call the viewSaveToImage(mingpianView); method.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.