packagecom.iceman.printscreen;

importjava.io.File;
importjava.io.FileOutputStream;

importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.Bitmap.Config;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.view.Display;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.WindowManager;
importandroid.widget.Button;
importandroid.widget.LinearLayout;
importandroid.widget.Toast;

publicclassPrintScreenDemoActivityextendsActivity{
privateButtonmButton;
privateLinearLayoutmLayout;
privateintmPrintNum;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
LayoutInflaterinf=this.getLayoutInflater();
mLayout=(LinearLayout)inf.inflate(R.layout.main,null);
setContentView(mLayout);
mButton=(Button)findViewById(R.id.print_btn);
mButton.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
GetandSaveCurrentImage();
mPrintNum++;
mButton.setText("截屏次数:"+mPrintNum);
}
});
}
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()+"/PrintScreenDemo/ScreenImage";

//3.保存Bitmap
try{
Filepath=newFile(SavePath);
//文件
Stringfilepath=SavePath+"/Screen_"+mPrintNum+".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(this,"截屏文件已保存至SDCard/PrintScreenDemo/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();
}
} 布局文件:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<Button
android:id="@+id/print_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="截屏"/>

</LinearLayout> 由于是把截屏的图片存放到sd卡里,所以要在manifest.xml文件中增加对sd卡的写入权限.


陈雨晨 2012-04-12 21:56 发表评论

更多相关文章

  1. android java 检测文件夹(目录)是否存在,不存在则创建
  2. android异步操作AsyncTask编写文件查看器
  3. android 之 选择文件
  4. Android http文件上传-本地+服务器一条龙分析
  5. Android 可選文件格式瀏覽器
  6. Android操作SD卡文件
  7. Android 之 将RAW资源文件写入SD卡工具类
  8. Android 上传文件[转]
  9. 挂载android只读文件系统为可读写

随机推荐

  1. Android(安卓)Studio使用新的Gradle构建
  2. Android实现截屏,将截图文件保存到本地文
  3. Android顶部导航之TabLayout和ViewPager
  4. 实现android 前后摄像头切换效果
  5. Android(安卓)P联网报错Cleartext HTTP t
  6. (转)android GPS
  7. android webview用法小结1
  8. Android(安卓)AlarmManager简单使用
  9. android 模拟器访问本地服务器接口api
  10. Android入门——页面跳转