Android 截屏 保存view的显示成图片到本地

 截屏方法


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.Display;
import android.view.View;

import java.io.IOException;

/**
 * @param
 * @Comments : 截屏
 * @Author : Lampo
 * @CreateDate : 2018/11/5 15:17
 * @ModifiedBy : Lampo
 * @ModifiedDate : 2018/11/5 15:17
 * @Modified : 6.0以上使用前要申请权限 Manifest.permission.WRITE_EXTERNAL_STORAGE
 *             Manifest.permission.READ_EXTERNAL_STORAGE
 */

public class ScreenShotsUtils {


    public static boolean screenShots(Activity activity) throws IOException {
        return screenShots(activity, 0, 0);
    }

    public static boolean screenShotsWidth(Activity activity, int width) throws IOException {
        return screenShots(activity, width, 0);
    }

    public static boolean screenShotsHeight(Activity activity, int height) throws IOException {
        return screenShots(activity, 0, height);
    }

    public static boolean screenShots(Activity activity, int width, int height) throws IOException {
        boolean flag = false;
        // 获取windows中最顶层的view
        View view = activity.getWindow().getDecorView();
        view.buildDrawingCache();
        // 获取状态栏高度
        Rect rect = new Rect();
        view.getWindowVisibleDisplayFrame(rect);
        int statusBarHeights = rect.top;
        Display display = activity.getWindowManager().getDefaultDisplay();
        // 获取屏幕宽和高
        int widths = display.getWidth();
        int heights = display.getHeight();
        // 允许当前窗口保存缓存信息
        view.setDrawingCacheEnabled(true);
        // 去掉状态栏
        Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), width,
                statusBarHeights + height, widths - width, heights - statusBarHeights - height);
        // 销毁缓存信息
        view.destroyDrawingCache();
        if (bmp != null) {
            final String path = FileUtils.getImageDownloadPath(activity);
            flag = FileUtils.saveFile(bmp, path);
        }
        return flag;
    }

    /**
     * 保存一个view的截图
     *
     * @param context
     * @param view
     * @return
     */
    public static boolean screenShots(Context context, View view) {
        boolean flag = false;
        Bitmap bitmap = getCacheBitmap(view);
        if (bitmap != null) {
            final String path = FileUtils.getImageDownloadPath(context);
            flag = FileUtils.saveFile(bitmap, path);
        }
        return flag;
    }

    /**
     * 获取一个 View 的缓存视图
     *
     * @param view
     * @return
     */
    public static Bitmap getCacheBitmap(View view) {
        final boolean drawingCacheEnabled = true;
        view.setDrawingCacheEnabled(drawingCacheEnabled);
        view.buildDrawingCache(drawingCacheEnabled);
        final Bitmap drawingCache = view.getDrawingCache();
        Bitmap bitmap;
        if (drawingCache != null) {
            bitmap = Bitmap.createBitmap(drawingCache);
            view.setDrawingCacheEnabled(false);
        } else {
            bitmap = null;
        }
        return bitmap;
    }

}

保存到路径

 /**
     * 随机生成一个文件,用于存放下载的图片
     *
     * @param context Context
     * @return
     */
    public static String getImageDownloadPath(Context context) {
        String imageRootDir = getImageDownloadDir(context);
        File dir = new File(imageRootDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        return dir + File.separator + fileName;
    }
/**
     * 获取下载图片文件存放的目录
     *
     * @param context Context
     * @return SDCard卡或者手机内存的根路径
     */
    public static String getImageDownloadDir(Context context) {
        return getRootDir(context) + IMAGE_DOWNLOAD_IMAGES_PATH;
    }
/**
     * 获取SDCard卡或者手机内存的根路径(优先获取SDCard卡的根路径)
     *
     * @param context Context
     * @return SDCard卡或者手机内存的根路径
     */
    public static String getRootDir(Context context) {
        String rootDir;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            rootDir = Environment.getExternalStorageDirectory().getAbsolutePath() + ROOT_DIR_PATH;
        } else {
            rootDir = context.getApplicationContext().getCacheDir().getAbsolutePath() + ROOT_DIR_PATH;
        }
        return rootDir;
    }

保存图片并且通知相册进行更新

  /**
     * 保存文件
     *
     * @param bm
     * @param fileName
     * @throws IOException
     */
    public static boolean saveFile(Bitmap bm, String fileName) {
        boolean flag = false;
        File myCaptureFile = new File(fileName);
        FileOutputStream bos = null;
        try {
            bos = new FileOutputStream(myCaptureFile);
            flag = bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
            bos.flush();
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Uri uri = Uri.fromFile(myCaptureFile);
        AICashApplication.getApplication().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));

        return flag;
    }

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值