screenshot To File With Wallpaper - Android Phone

Android examples for Phone:Screen

Description

screenshot To File With Wallpaper

Demo Code

/*//from w  w w  .  j a  v  a  2 s .c om
 * Copyright (c) 2014 Zhang Hai <Dreaming.in.Code.ZH@Gmail.com>
 * All Rights Reserved.
 */
//package com.java2s;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.View;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static boolean screenshotToFileWithWallpaper(Activity activity,
            File file, Bitmap.CompressFormat compressFormat, int quality) {
        return saveBitmap(screenshotWithWallpaper(activity), file,
                compressFormat, quality);
    }

    public static boolean screenshotToFileWithWallpaper(Activity activity,
            File file) {
        return screenshotToFileWithWallpaper(activity, file,
                Bitmap.CompressFormat.JPEG, 95);
    }

    public static boolean saveBitmap(Bitmap snapshot, File file,
            Bitmap.CompressFormat compressFormat, int quality) {
        try {
            FileOutputStream snapshotOutput = new FileOutputStream(file);
            if (!snapshot.compress(compressFormat, quality, snapshotOutput)) {
                return false;
            }
            snapshotOutput.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Bitmap screenshotWithWallpaper(Context context,
            View view, Rect clipRect) {

        Bitmap bitmap = Bitmap.createBitmap(clipRect.width(),
                clipRect.height(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        // Translate for clipRect.
        canvas.translate(-clipRect.left, -clipRect.top);

        Drawable wallpaper = WallpaperManager.getInstance(context)
                .getFastDrawable();
        // Center wallpaper on screen, as in launcher.
        DisplayMetrics displayMetrics = view.getResources()
                .getDisplayMetrics();
        wallpaper.setBounds(0, 0, displayMetrics.widthPixels,
                displayMetrics.heightPixels);
        // Translate the canvas to draw wallpaper on the correct location.
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        canvas.save();
        canvas.translate(-location[0], -location[1]);
        wallpaper.draw(canvas);
        canvas.restore();

        view.draw(canvas);

        return bitmap;
    }

    public static Bitmap screenshotWithWallpaper(Activity activity) {
        //View activityView = activity.findViewById(android.R.id.content).getRootView();
        View activityView = activity.getWindow().getDecorView();
        Rect frame = new Rect();
        activityView.getWindowVisibleDisplayFrame(frame);
        return screenshotWithWallpaper(activity, activityView, frame);
    }
}

Related Tutorials