Android screenshot Key Code

Source: Internet
Author: User

[Java]View Plaincopy
  1. Package com.wangzhen.util;
  2. Import Java.io.File;
  3. Import java.io.FileNotFoundException;
  4. Import Java.io.FileOutputStream;
  5. Import java.io.IOException;
  6. Import android.app.Activity;
  7. Import Android.graphics.Bitmap;
  8. Import Android.graphics.Rect;
  9. Import Android.view.View;
  10. Public class Screenshot {
  11. //Take screenshots
  12. private static Bitmap takescreenshot (activity activity) {
  13. //view is the view that will be snapped
  14. View view = Activity.getwindow (). Getdecorview ();
  15. View.setdrawingcacheenabled (true);
  16. View.builddrawingcache ();
  17. Bitmap Bitmap = View.getdrawingcache ();
  18. //Get status bar height
  19. Rect rect = new rect ();
  20. Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (rect);
  21. int statusbarheight = rect.top;
  22. //Get screen length and height
  23. int width = Activity.getwindowmanager (). Getdefaultdisplay (). GetWidth ();
  24. int height = Activity.getwindowmanager (). Getdefaultdisplay ()
  25. . GetHeight ();
  26. //Remove title bar
  27. Bitmap bitmap_final = Bitmap.createbitmap (Bitmap, 0, Statusbarheight,
  28. width, height-statusbarheight);
  29. View.destroydrawingcache ();
  30. return bitmap_final;
  31. }
  32. //Save
  33. private static void Savepic (Bitmap Bitmap, File filepath) {
  34. FileOutputStream fileoutputstream = null;
  35. try {
  36. FileOutputStream = new FileOutputStream (filepath);
  37. if (null! = FileOutputStream) {
  38. Bitmap.compress (Bitmap.CompressFormat.PNG,
  39. FileOutputStream);
  40. Fileoutputstream.flush ();
  41. Fileoutputstream.close ();
  42. }
  43. } catch (FileNotFoundException e) {
  44. //TODO auto-generated catch block
  45. E.printstacktrace ();
  46. System.out.println (E.getmessage ());
  47. } catch (IOException e) {
  48. //TODO auto-generated catch block
  49. E.printstacktrace ();
  50. }
  51. }
  52. //External call Method
  53. public static void shoot (activity activity, File FilePath) {
  54. if (FilePath = = null) {
  55. return;
  56. }
  57. if (!filepath.exists ()) {
  58. Filepath.getparentfile (). mkdir ();
  59. }
  60. Screenshot.savepic (Screenshot.takescreenshot (activity), filePath);
  61. }

Android screenshot Key Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.