android实现屏幕截图并保存(去除状态栏)
2013-01-31 17:38
357 查看
android屏幕截图
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;
public class ScreenShot {
// 获取指定Activity的截屏,保存到png文件
private static Bitmap takeScreenShot(Activity activity){
//View是你需要截图的View
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
//获取状态栏高度
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
System.out.println(statusBarHeight);
//获取屏幕长和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
//去掉标题栏
//Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return b;
}
//保存到sdcard
private static void savePic(Bitmap b,String strFileName){
FileOutputStream fos = null;
try {
fos = new FileOutputStream(strFileName);
if (null != fos)
{
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//程序入口
public static void shoot(Activity a){
ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;
public class ScreenShot {
// 获取指定Activity的截屏,保存到png文件
private static Bitmap takeScreenShot(Activity activity){
//View是你需要截图的View
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
//获取状态栏高度
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
System.out.println(statusBarHeight);
//获取屏幕长和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
//去掉标题栏
//Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return b;
}
//保存到sdcard
private static void savePic(Bitmap b,String strFileName){
FileOutputStream fos = null;
try {
fos = new FileOutputStream(strFileName);
if (null != fos)
{
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//程序入口
public static void shoot(Activity a){
ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");
}
}
相关文章推荐
- Android 保存图片到系统图库 -- 利用 “系统保存屏幕截图” 的方法实现
- Android 整个手机屏幕截图和去除状态栏截图
- Android屏幕截图实现 (adbd部分)
- Android 5.0及以上实现屏幕截图
- 获取和保存当前屏幕的截图 实现的C++代码如下
- android 实现屏幕截图
- Unity3d截图保存到Android相册的实现
- Android 屏幕截图(底层实现方式)
- Android 5.0及以上编程实现屏幕截图功能的方法
- Android屏幕截图实现
- Android双击屏幕动态实现全屏(隐藏标题栏状态栏)
- Android 代码实现屏幕截图功能
- WinCE实现屏幕截图并保存为BMP文件
- Android开发之屏幕截图保存至SD卡
- Android-屏幕截图功能实现
- 【Android】Android实现截取当前屏幕图片并保存至SDCard
- Android中实现截图保存到指定目录或者系统图片,并支持批量删除
- Android中通过view方式获取当前Activity的屏幕截图实现方法