Android 实现简单截屏并保存为文件

openkk 12年前
    /**       * 截屏       * @param v   视图       * @param filePath  保存路径       */      private void getScreenHot(View v, String filePath)      {                  try          {           Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Config.ARGB_8888);           Canvas canvas = new Canvas();           canvas.setBitmap(bitmap);           v.draw(canvas);             try           {            FileOutputStream fos = new FileOutputStream(filePath);            bitmap.compress(CompressFormat.PNG, 100, fos);           }           catch (FileNotFoundException e)           {            throw new InvalidParameterException();           }            }          catch (Exception e)          {            e.printStackTrace();          }      }
调用方法:
getScreenHot((View) getWindow().getDecorView(), "/sdcard/test1.png");