android---实现截屏并保存到SD卡显示在系统相册中

布局就是一个截图按钮,没有什么比较复杂的东西,直接上代码

方法一 

1.在AndroidManifest中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2.获取整改窗口的截图方法

/**
 * 获取整个窗口的截图
 *
 * @param context
 * @return
 */
@SuppressLint("NewApi")
private Bitmap captureScreen(Activity context) {
    View cv = context.getWindow().getDecorView();
    cv.setDrawingCacheEnabled(true);
    cv.buildDrawingCache();
    Bitmap bmp = cv.getDrawingCache();
    if (bmp == null) {
        return null;
    }
    bmp.setHasAlpha(false);
    bmp.prepareToDraw();
    return bmp;
}

3.保存图片的方法

新建一个文件把图片保存到这个文件夹中,利用系统提供的api插入到系统相册,最后通知相册更新。具体方法如下:

 public static void saveImageToGallery(Activity context, Bitmap bmp) {
        String local_path = Parser.getSdCard() + Const.PATH + Const.IMGAGE_PATH ;
        // 创建文件夹
//        File appDir = new File(Environment.getExternalStorageDirectory(), "imageok");
        File appDir = new File(local_path, "imageok");
        //判断不存在就创建
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        //以时间命名
        String fileName = System.currentTimeMillis() + ".png";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(),
                    file.getAbsolutePath(), fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 最后通知图库更新
        String path = Environment.getExternalStorageDirectory().getPath();
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
    }
调用
//方法2  有的机型相册中无法找到图片暂时保存
//                Bitmap bitmap = captureScreen(this);
//                saveImageToGallery(this,bitmap);

方法二


/**
 * 截取全屏
 * @return
 */
public Bitmap captureScreenWindow() {
    getWindow().getDecorView().setDrawingCacheEnabled(true);
    Bitmap bmp = getWindow().getDecorView().getDrawingCache();
    return bmp;
}
 
 
/**
 * 保存到内存卡
 *
 * @param bitName
 * @param mBitmap
 */
public void saveBitmapForSdCard(Activity context, String bitName, Bitmap mBitmap) {
    String local_path = Parser.getSdCard() + Const.PATH + Const.IMGAGE_PATH;
    File appDir = new File(local_path);
    //判断不存在就创建
    if (!appDir.exists()) {
        appDir.mkdir();
    }
    //创建file对象
    File f = new File(local_path + bitName + ".png");
    try {
        //创建
        f.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    //原封不动的保存在内存卡上
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 其次把文件插入到系统图库
    try {
        MediaStore.Images.Media.insertImage(context.getContentResolver(),
                f.getAbsolutePath(), bitName, null);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    // 最后通知图库更新
    String path = Environment.getExternalStorageDirectory().getPath();
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
    IToast.toast("截屏成功,请在相册中查看!");
}

调用方式
  
long time = System.currentTimeMillis() / 1000;
Bitmap bitmap = captureScreenWindow();
saveBitmapForSdCard(this, time + "", bitmap);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值