Android screenshot code note

Source: Internet
Author: User

Static

View code

 Import  Java. Io. filenotfoundexception;  Import  Java. Io. fileoutputstream;  Import  Java. Io. ioexception;  Import  Android. App. activity;  Import  Android. Graphics. Bitmap;  Import  Android. Graphics. rect;  Import Android. Media. mediametadataretriever;  Import  Android. util. log;  Import  Android. View. view;  Public   Class  Screenshot {  //  Obtains screenshots of a specified activity and saves them to PNG files.      Static  Bitmap takescreenshot (activity) {log. I ( "Tag", "tackscreenshot" );  // View is the view you need View view = Activity. getwindow (). getdecorview (); view. setdrawingcacheenabled (  True  ); View. builddrawingcache (); bitmap B1 = View. getdrawingcache ();  //  Retrieve the height of the status bar Rect frame = New  Rect (); activity. getwindow (). getdecorview (). getwindowvisibledisplayframe (FRAME );  Int Statusbarheight = Frame. Top; log. I ( "Tag", "statusbarheight =" + Statusbarheight );  //  Get screen length and height          Int Width = Activity. getwindowmanager (). getdefadisplay display (). getwidth ();  Int Height = Activity. getwindowmanager (). getdefadisplay display (). getheight ();  //  Remove question bar  //  Bitmap B = bitmap. createbitmap (B1, 0, 25,320,455 ); Bitmap B = bitmap. createbitmap (B1, 0, Statusbarheight, width, height - Statusbarheight); view. destroydrawingcache ();  Return  B ;}  //  Save to sdcard      Private   Static   Void  Savepic (Bitmap B, string strfilename) {fileoutputstream fos = Null  ;  Try {Log. I ( "Tag", "Start savepic" ); Fos = New  Fileoutputstream (strfilename); log. I ( "Tag", "strfilename =" + Strfilename );  If ( Null ! = FOS) {B. Compress (bitmap. compressformat. PNG, 90 , FOS); FOS. Flush (); FOS. Close (); log. I ( "Tag", "Save pic" );}} Catch  (Filenotfoundexception e) {e. printstacktrace ();}  Catch  (Ioexception e) {e. printstacktrace ();}}  Public   Static   Void  Shoot (Activity A, string B ){  //  Screenshot. savepic (screenshot. takescreenshot (a), "sdcard/xx.png "); Log. I ("tag", "shot" ); Bitmap bitmap =Screenshot. takescreenshot (a); screenshot. savepic (bitmap, B );}} 

 

Dynamic

View code

     Public  Bitmap createvideothumbnail (string filepath) {Bitmap bitmap = Null  ; Android. Media. mediametadataretriever retriever = New  Android. Media. mediametadataretriever ();  Try { //  Mode_capture_frame_only //  Retriever  //  . Setmode (Android. Media. mediametadataretriever. mode_capture_frame_only );  //  Retriever. setmode (mediametadataretriever. mode_capture_frame_only );  Retriever. setdatasource (filepath );  //  Bitmap = retriever. captureframe (); String timestring = Retriever. extractmetadata (mediametadataretriever. metadata_key_duration );  Long Time = long. parselong (Time string) * 1000 ; Log. I ( "Tag", "time =" + Time); bitmap = Retriever. getframeattime (time * 31/160 ); //  Frame selection by video length ratio } Catch  (Illegalargumentexception ex ){  //  Assume this is a temporary upt video file } Catch  (Runtimeexception ex ){  // Assume this is a temporary upt video file. } Finally  {  Try  {Retriever. Release ();}  Catch  (Runtimeexception ex ){  //  Ignore failures while cleaning up.  }}  Return  Bitmap ;} 

 

Related Article

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.