首页 > 移动平台 > 详细

android截屏:保存一个view的内容为图片并存放到SD卡

时间:2014-10-18 16:53:16      阅读:307      评论:0      收藏:0      [点我收藏+]

项目中偶尔会用到截屏分享,于是就有了下面这个截屏的方法~

下面得saveImage()方法就是保存当前Activity对应的屏幕所有内容的截屏保存。

private void saveImage() {

 

// SD卡保存路径

String savePath = Environment.getExternalStorageDirectory() + "/temp.png";

// showProgress("请稍候", "正在保存图片……");

saveMyBitmap(getBitmapFromRootView(getWindow().getDecorView()), savePath);

}

 

// 获取view并转换成bitmap图片

private static Bitmap getBitmapFromRootView(View view) {


view.setDrawingCacheEnabled(true);

Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());

view.setDrawingCacheEnabled(false);

if (bmp != null) {

return bmp;

} else {

return null;

}

}

 

// 把bitmao图片保存到对应的SD卡路径中

private void saveMyBitmap(Bitmap mBitmap, String path) {

File f = new File(path);

try {

f.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

FileOutputStream fOut = null;

try {

fOut = new FileOutputStream(f);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

if (mBitmap != null) {

// 保存格式为PNG 质量为100

mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);

}

try {

fOut.flush();

} catch (IOException e) {

e.printStackTrace();

}

try {

fOut.close();

} catch (IOException e) {

e.printStackTrace();

}

}

android截屏:保存一个view的内容为图片并存放到SD卡

原文:http://www.cnblogs.com/xgjblog/p/4033269.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!