Android的屏幕截图? [英] android screenshot?

查看:154
本文介绍了Android的屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

      Button button = (Button)findViewById(R.id.btnTakeScreenshot);
      button.setOnClickListener(new View.OnClickListener() {
      //@Override
      public void onClick(View v) {
      final View content = findViewById(R.id.layoutroot);
      content.setDrawingCacheEnabled(true);
       Bitmap bitmap = content.getDrawingCache();
       File file = new File(Environment.getExternalStorageDirectory() + "/test.png");
       try 
       {
           file.createNewFile();
           FileOutputStream ostream = new FileOutputStream(file);
           bitmap.compress(CompressFormat.PNG, 100, ostream);
           ostream.close();
           Toast.makeText(content.getContext(), "HELLO", Toast.LENGTH_SHORT);
       } 
       catch (Exception e) 
       {
        System.out.print("error");
           e.printStackTrace();
       }
}
});

以上code是捕捉到的截图,但它创造零KB空白文件?

above code is to capture a screenshot, but it creates blank file of zero kb?

推荐答案

file.createNewFile(); 不正是你问它:创建一个空白,空文件。然后,如果图形缓存为空,你会得到什么的文件中。试试这个:

file.createNewFile(); does exactly what you asked of it: creates a blank, empty file. Then if the drawing cache is empty you will get nothing in the file. Try this:

Button button = (Button)findViewById(R.id.btnTakeScreenshot);
      final View content = findViewById(R.id.layoutroot);
      content.setDrawingCacheEnabled(true);
      button.setOnClickListener(new View.OnClickListener() {
      //@Override
      public void onClick(View v) {
       Bitmap bitmap = content.getDrawingCache();
       File file = new File(Environment.getExternalStorageDirectory() + "/test.png");
       try 
       {
           file.createNewFile();
           FileOutputStream ostream = new FileOutputStream(file);
           bitmap.compress(CompressFormat.PNG, 100, ostream);
           ostream.close();
           Toast.makeText(content.getContext(), "HELLO", Toast.LENGTH_SHORT);
       } 
       catch (Exception e) 
       {
        System.out.print("error");
           e.printStackTrace();
       }
}
});

这篇关于Android的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆