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