您的位置:首页 > 其它

安卓开发_实现截图功能

2014-12-06 22:55 423 查看
网上百度了很多,大部分相当复杂,对于我这个水平来说,目前还是无法消化的,寻找了几天,终于找到一个简单的实现方法。

其实就是一个函数,这个函数实现了截图功能

/**
* 获取和保存当前屏幕的截图
*/
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"/>


具体使用就是点击一个按钮,在按钮的响应事件里调用这个函数就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐