开发者

android screenshot? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
      Button button = (Button)findViewById(R.id.btnTak开发者_高级运维eScreenshot);
      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();
       }
}
});

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜