美文网首页
Android实现截屏,将截图文件保存到本地文件夹

Android实现截屏,将截图文件保存到本地文件夹

作者: 浮世绘町 | 来源:发表于2020-07-12 15:22 被阅读0次

    Android实现对当前界面截屏,并将截图文件存放至本地文件夹

    首先需要动态申请两项权限(Android6.0后危险权限之类的都需要动态申请),在AndroidManifest.xml中静态添加

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    其中<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>会报错提示

    Permission is only granted to system apps,解决方案:

    点击File—>Settings—>Editor—>Inspections—>搜索Using system app permission,将Error改为Waring。

    解决以后再Acrivity中动态申请该权限,调用以下代码

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
    Manifest.permission.READ_EXTERNAL_STORAGE,
    Manifest.permission.WRITE_EXTERNAL_STORAGE};

    public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity,
    Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE);
    }
    

    }
    然后通过创建一个ScreenShot的工具类来进行管理和操作

    package com.example.administrator.screenshot.util;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.Rect;
    import android.os.Environment;
    import android.util.Log;
    import android.view.View;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class ScreenShot {
    // 获取指定Activity的截屏,保存到png文件
    static String newFilePath;
    private static Bitmap takeScreenShot(Activity activity) {
    // View是你需要截图的View
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap b1 = view.getDrawingCache();

        // 获取状态栏高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        Log.i("TAG", "" + statusBarHeight);
    
        // 获取屏幕长和高
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay()
                .getHeight();
        // 去掉标题栏
        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
                - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }
    
    // 保存到sdcard
    private static void savePic(Bitmap b, String strFileName) {
        FileOutputStream fos = null;
        try {
    

    //判断是否存在该文件夹和文件,如果没有新建
    File file = new File(strFileName);
    //获取父目录
    File fileParent = file.getParentFile();
    //判断是否存在
    if (!fileParent.exists()) {
    // 创建父目录文件
    fileParent.mkdirs();
    }
    file.createNewFile();
    fos = new FileOutputStream(new File(strFileName));
    if (null != fos) {
    b.compress(Bitmap.CompressFormat.PNG, 90, fos);
    fos.flush();
    fos.close();
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    //用于获取当前最新的截图
    public static String getPath(){
        return newFilePath;
    }
    // 截屏方法
    public static void shoot(Activity a) {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fileName = df.format(date)+".png";
        String sdCardPath = Environment.getExternalStorageDirectory().getPath();
        String filePath = sdCardPath + File.separator + "xinlv" + File.separator + "Screenshoots"+ File.separator + fileName;
        newFilePath = filePath;
        ScreenShot.savePic(ScreenShot.takeScreenShot(a), filePath);
    }
    

    }
    做了一个小Demo实现了本功能,Demo地址https://github.com/18855578562/ScreenShot

    相关文章

      网友评论

          本文标题:Android实现截屏,将截图文件保存到本地文件夹

          本文链接:https://www.haomeiwen.com/subject/bzogcktx.html