Android实现截屏并保存操作功能
作者:cjjky 时间:2021-12-16 19:45:16
该篇文章是说明在Android手机或平板电脑中如何实现截取当前屏幕的功能,并把截取的屏幕保存到SDCard中的某个目录文件夹下面。
实现的代码如下:
/**
* 获取和保存当前屏幕的截图
*/
private void GetandSaveCurrentImage()
{
//1.构建Bitmap
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();
Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
//2.获取屏幕
View decorview = this.getWindow().getDecorView();
decorview.setDrawingCacheEnabled(true);
Bmp = decorview.getDrawingCache();
String SavePath = getSDCardPath()+"/AndyDemo/ScreenImage";
//3.保存Bitmap
try {
File path = new File(SavePath);
//文件
String filepath = SavePath + "/Screen_1.png";
File file = new File(filepath);
if(!path.exists()){
path.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = null;
fos = new FileOutputStream(file);
if (null != fos) {
Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
Toast.makeText(mContext, "截屏文件已保存至SDCard/AndyDemo/ScreenImage/下", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取SDCard的目录路径功能
* @return
*/
private String getSDCardPath(){
File sdcardDir = null;
//判断SDCard是否存在
boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(sdcardExist){
sdcardDir = Environment.getExternalStorageDirectory();
}
return sdcardDir.toString();
}
由于要对SDCard进行操作,所以别忘记了在manifest.xml文件中赋以对SDCard的读写权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
希望本文所述对大家学习Android软件编程有所帮助。
标签:Android,截屏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring使用OXM进行对象XML映射解析
2022-11-11 16:05:19
![](https://img.aspxhome.com/file/2023/1/60661_0s.jpg)
SpringBoot v2.2以上重复读取Request Body内容的解决方案
2022-06-27 11:00:16
C#实现控制电脑注销,关机和重启
2021-10-28 14:21:15
![](https://img.aspxhome.com/file/2023/9/76429_0s.png)
C#定时关闭窗体实例
2023-07-09 22:53:25
Nginx启用压缩及开启gzip 压缩的方法
2021-09-20 21:19:25
![](https://img.aspxhome.com/file/2023/8/66628_0s.png)
SpringBoot中shiro过滤器的重写与配置详解
2021-07-28 23:40:09
Java发送邮箱验证码、session校验功能
2023-09-11 02:44:21
C# SqlHelper应用开发学习
2023-05-07 11:07:35
Java 实现微信和支付宝支付功能
2023-03-08 23:18:04
Java实现图片倒影的源码实例内容
2022-08-30 02:39:24
![](https://img.aspxhome.com/file/2023/2/65162_0s.jpg)
Java 中Comparable和Comparator区别比较
2023-10-28 23:16:34
java中使用zxing批量生成二维码立牌
2021-12-31 04:31:03
![](https://img.aspxhome.com/file/2023/1/64371_0s.png)
java的Arrays工具类实战
2023-08-21 15:46:36
![](https://img.aspxhome.com/file/2023/6/58076_0s.png)
springboot openfeign从JSON文件读取数据问题
2023-11-09 15:55:55
详解直接插入排序算法与相关的Java版代码实现
2022-06-13 09:06:38
![](https://img.aspxhome.com/file/2023/5/72635_0s.png)
Java实现简单LRU缓存机制的方法
2023-08-11 03:54:49
![](https://img.aspxhome.com/file/2023/7/83057_0s.png)
Java将对象保存到文件中/从文件中读取对象的方法
2022-06-18 21:26:42
Java杂谈之重复代码是什么
2022-06-08 15:07:24
一篇文章带你了解XGBoost算法
2021-10-10 01:14:45
![](https://img.aspxhome.com/file/2023/2/61082_0s.jpg)
详解Java二叉排序树
2022-05-25 03:14:26
![](https://img.aspxhome.com/file/2023/8/83018_0s.png)