Android Bitmap保存およびスクリーンキャプチャツールクラス



import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.View;

import com.acmenxd.logger.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author AcmenXD
 * @version v1.0
 * @github https://github.com/AcmenXD
 * @date 2016/11/23 16:09
 * @detail Bitmap   
 */
public class BitmapUtils {

    /**
     *     (  )   
     *
     * @param pActivity
     * @param isSaveStatusBar        
     * @param pSavePath           
     */
    public static void saveScreenAsImage(Activity pActivity, boolean isSaveStatusBar, File pSavePath) {
        View pView = pActivity.getWindow().getDecorView();
        pView.setDrawingCacheEnabled(true);
        pView.buildDrawingCache();
        Bitmap srcBitmap = pView.getDrawingCache();
        //     
        Point point = new Point();
        pActivity.getWindowManager().getDefaultDisplay().getSize(point);
        int width = pView.getWidth();//point.x;
        int height = pView.getHeight();//point.y;
        //      
        int statusBarHeight = 0;
        if (isSaveStatusBar == false) {
            Rect frame = new Rect();
            pActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
            statusBarHeight = frame.top;
            height = height - statusBarHeight;
        }
        //      bitmap
        Bitmap bitmap = Bitmap.createBitmap(srcBitmap, 0, statusBarHeight, width, height);
        //        ,      
        pView.destroyDrawingCache();
        //     
        saveBitmap(bitmap, pSavePath);
    }

    /**
     *          ->         
     * *         JPEG     
     */
    public static void saveBitmap(Bitmap pBitmap, File savePath) {
        saveBitmap(pBitmap, savePath, String.valueOf(RandomUtils.getRandomByTime()) + ".jpg", null);
    }

    /**
     *         
     *
     * @param pBitmap     bitmap  
     * @param savePath     
     * @param fileName     (    )
     * @param format        (   JPEG   )
     */
    public static void saveBitmap(Bitmap pBitmap, File savePath, String fileName, Bitmap.CompressFormat format) {
        if (format == null) {
            format = Bitmap.CompressFormat.JPEG;
        }
        //     
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(savePath, fileName));
            if (fos != null) {
                pBitmap.compress(format, 100, fos);
                fos.flush();
            }
        } catch (IOException pE) {
            Logger.e(pE);
        } finally {
            IOUtils.closeQuietly(fos);
        }
    }

}