Android中截取当前屏幕图片的实例代码
2023-06-13 09:15:04 时间
/**
*获取和保存当前屏幕的截图
*/
privatevoidGetandSaveCurrentImage()
{
//1.构建Bitmap
WindowManagerwindowManager=getWindowManager();
Displaydisplay=windowManager.getDefaultDisplay();
intw=display.getWidth();
inth=display.getHeight();
BitmapBmp=Bitmap.createBitmap(w,h,Config.ARGB_8888);
//2.获取屏幕
Viewdecorview=this.getWindow().getDecorView();
decorview.setDrawingCacheEnabled(true);
Bmp=decorview.getDrawingCache();
StringSavePath=getSDCardPath()+"/AndyDemo/ScreenImage";
//3.保存Bitmap
try{
Filepath=newFile(SavePath);
//文件
Stringfilepath=SavePath+"/Screen_1.png";
Filefile=newFile(filepath);
if(!path.exists()){
path.mkdirs();
}
if(!file.exists()){
file.createNewFile();
}
FileOutputStreamfos=null;
fos=newFileOutputStream(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(Exceptione){
e.printStackTrace();
}
}
/**
*获取SDCard的目录路径功能
*@return
*/
privateStringgetSDCardPath(){
FilesdcardDir=null;
//判断SDCard是否存在
booleansdcardExist=Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(sdcardExist){
sdcardDir=Environment.getExternalStorageDirectory();
}
returnsdcardDir.toString();
}
由于要对SDCard进行操作,所以别忘记了在manifest.xml文件中赋以对SDCard的读写权限:
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
相关文章
- 加密狗android,Android系统加密狗的设计与实现
- android 混淆不起作用,Android代码混淆的写法总结
- android开发笔记之 Android代码混淆打包
- mac 电脑android环境变量设置,mac上Android环境变量配置[通俗易懂]
- android activity singletask,Android Activity启动模式之singleTask实例详解
- android 验证码短信验证码,Android短信验证码倒计时验证的2种常用方式
- android scaleanimation动画,Android 的ScaleAnimation 缩放动画基本运用
- android系统中toast是什么_Android个人资料简单布局
- android登录注册跳转的代码_Android开发代码
- Connecting Android to Oracle: The Ultimate Guide for Seamless Integration.(android连接oracle)
- Android中获取电池电量实例代码
- Android简单的利用MediaRecorder进行录音的实例代码
- Android之RAS加密算法测试实例
- Android字符串转Ascii码实例代码
- Android创建Menu菜单实例
- Android中使用IntentService创建后台服务实例
- Android根据电话号码获得联系人头像实例代码