private void printScreen(boolean save) {//截屏View view = this.getWindow().getDecorView();//this是当前的Activity// if (view.isDrawingCacheEnabled()) {view.setDrawingCacheEnabled(true);Calendar c = Calendar.getInstance();String date = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "  " + c.get(Calendar.HOUR_OF_DAY) + "-" + c.get(Calendar.MINUTE) + "-" + c.get(Calendar.SECOND);// }view.buildDrawingCache();Bitmap bmp = view.getDrawingCache();imagePath = ConstValue.MY_ALBUM_DIR + "/" + date + ".jpg";//路径writePhotoJpg(bmp, imagePath);// FileSaveAsync myAsync = new FileSaveAsync(bmp, imagePath, true);// myAsync.execute();}

保存到sd卡下的方法:(记得加上写入sd卡的权限)

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>

public void writePhotoJpg(Bitmap data, String pathName) {File file = new File(pathName);try {file.createNewFile();// BufferedOutputStream os = new BufferedOutputStream(// new FileOutputStream(file));FileOutputStream os = new FileOutputStream(file);data.compress(Bitmap.CompressFormat.JPEG, 100, os);os.flush();os.close();MyDebug.i("writePhotoJpg");} catch (Exception e) {e.printStackTrace();}}public void writePhotoPng(Bitmap data, String pathName) {File file = new File(pathName);try {file.createNewFile();FileOutputStream os = new FileOutputStream(file);// BufferedOutputStream os = new BufferedOutputStream(// new FileOutputStream(file));data.compress(Bitmap.CompressFormat.PNG, 100, os);os.flush();os.close();MyDebug.i("writePhotoPng");} catch (Exception e) {e.printStackTrace();}}

另外几种方法,没仔细测试过:仅供参考

 //抓屏的方式生成照片   public static Bitmap printScreen(Context context) {View view = ((Activity) context).getWindow().getDecorView();// if (view.isDrawingCacheEnabled()) {view.setDrawingCacheEnabled(true);//Calendar c = Calendar.getInstance();//String date = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "  " + c.get(Calendar.HOUR_OF_DAY) + "-" + c.get(Calendar.MINUTE) + "-" + c.get(Calendar.SECOND);// }view.buildDrawingCache();Bitmap bmp = view.getDrawingCache();return bmp;/*imagePath = ConstValue.MY_ALBUM_DIR + "/" + date + ".jpg";ImageFile.writePhotoJpg(bmp, imagePath);*/// FileSaveAsync myAsync = new FileSaveAsync(bmp, imagePath, true);// myAsync.execute();}         public static Bitmap catchScreen(View v1) {        v1.setDrawingCacheEnabled(true);        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());        v1.setDrawingCacheEnabled(false);        return bitmap;          }



更多相关文章

  1. android 开发环境搭建
  2. android recovery升级原理及update.zip升级过程分析
  3. Android存储使用参考
  4. android各种图片获取路径的方式
  5. Android(安卓)签名证书
  6. android 生成二维码
  7. Android.API.Context.getFilesDir()
  8. android 7.0 制作ota差分包
  9. android 开发环境搭建

随机推荐

  1. 【Android Native Code开发系列】一 前言
  2. Android(安卓)Handler的使用(二)
  3. Android之父安迪·鲁宾
  4. 教你如何在 Android 使用多线程下载文件
  5. Android上面TreeView效果
  6. Android之隐式意图(Intent)如何查找匹配
  7. [Com]Java中的串口编程
  8. Android下的Hook初探
  9. Android ANR (转)
  10. Android Handler 消息传递机制