【问题标题】:android screenshot? [closed]安卓截图? [关闭]
【发布时间】:2011-06-22 08:19:48
【问题描述】:
      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();
       }
}
});

上面的代码是截取屏幕截图,但它会创建零kb的空白文件?

【问题讨论】:

  • 它会抛出任何异常吗?
  • 不...它不会抛出任何异常...问题在于保存/捕获图像..
  • 您似乎不会尝试自己解决该问题。
  • 看起来像 this 的副本,尽管使用的是不同的帐户。代码也是一样的...
  • 我们正在做同一个项目...我们没有得到答案,所以我再次提出答案..

标签: android button screenshot


【解决方案1】:

file.createNewFile(); 完全按照您的要求执行:创建一个空白的空文件。然后,如果绘图缓存为空,则文件中将一无所获。试试这个:

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();
       }
}
});

【讨论】:

  • 嘿,伙计......它工作......很好的解决方案 femi......谢谢这么多
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
相关资源
最近更新 更多