安卓开发_实现截图功能
2014-12-06 22:55
423 查看
网上百度了很多,大部分相当复杂,对于我这个水平来说,目前还是无法消化的,寻找了几天,终于找到一个简单的实现方法。
其实就是一个函数,这个函数实现了截图功能
与函数对应的还需要添加一个系统权限
在AndroidManifest.xml文件下
具体使用就是点击一个按钮,在按钮的响应事件里调用这个函数就可以了。
其实就是一个函数,这个函数实现了截图功能
/** * 获取和保存当前屏幕的截图 */ private void GetandSaveCurrentImage() { //构建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 ); //获取屏幕 View decorview = this.getWindow().getDecorView(); decorview.setDrawingCacheEnabled(true); Bmp = decorview.getDrawingCache(); //图片存储路径 String SavePath = getSDCardPath()+"/qxbf/ScreenImages"; //这里是截图保存的路径 //保存Bitmap try { File path = new File(SavePath); Time time = new Time("GMT+8"); //这里求出了手机系统当前的时间,用来给截出的图片作为名字。否则名字相同,就只会产生一个图片,要想产生多个图片,便需要每个 图片的名字不同,我就用最水的办法,用系统时间来命名了 time.setToNow(); int year = time.year; int month = time.month; int day = time.monthDay; int minute = time.minute; int hour = time.hour; int sec = time.second; //文件 String filepath = SavePath+"/" + year+month+day+minute+sec+".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(Equip_pk_result.this, "截屏文件已保存至SDCard/qxbf/ScreenImages/目录下",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();
与函数对应的还需要添加一个系统权限
在AndroidManifest.xml文件下
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
具体使用就是点击一个按钮,在按钮的响应事件里调用这个函数就可以了。
相关文章推荐
- 安卓开发实现摇一摇功能
- 安卓之插件化开发使用DexClassLoader&AssetManager实现功能插件化
- 本人在安卓程序开发时用到的各种小功能(一) 屏幕截图
- 安卓开发实现短信验证码的功能
- Web开发之用canvas2image.js将canvas保存为图片(实现页面截图下载功能)
- 关于在安卓开发中百度地图功能不能实现定位。
- 安卓开发 :实现打开一次相机连续拍多张照片返回后拿到多张照片,一个小功能的构思
- 安卓开发利用闪光灯实现手电筒功能
- 安卓服务端开发(1) 安卓结合PHP实现连接数据库验证登录功能(附全部代码)
- 【安卓小笔记】开发过程的小功能实现
- 安卓开发实现手电筒关闭开启功能
- 安卓开发 类似于扣扣 删除消息 时,滑动调出选项的功能实现
- 本人在安卓程序开发时用到的各种小功能(一) 屏幕截图
- C#软件开发实例.私人订制自己的屏幕截图工具(四)基本截图功能实现
- C#软件开发实例.私人订制自己的屏幕截图工具(四)基本截图功能实现
- 安卓开发使用百度地图sdk实现定位,添加marker,城市切换等功能
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载续][重点推荐控件]
- 使用文本语音开发包实现语音朗读功能