---------- Screenshot Method
[Java]
Private Bitmap shot (){
View views = getWindow (). getDecorView ();
Views. buildDrawingCache ();
// Obtain the status bar height
Rect frames = new Rect ();
Views. getWindowVisibleDisplayFrame (frames );
Int statusBarHeights = frames. top;
Display display = getWindowManager (). getDefaultDisplay ();
Int widths = display. getWidth ();
Int heights = display. getHeight ();
// Method 1
Views. layout (0, statusBarHeights, widths, heights-statusBarHeights );
Views. setDrawingCacheEnabled (true); // allows the current window to save cache information. either of the two methods needs to be added
Bitmap bmp = Bitmap. createBitmap (views. getDrawingCache ());
// Method 2
// 1. source bitmap 2. The first pixel of X coordinate 3. The first pixel of Y y coordinate 4. The number of rows of width pixels in each row 5 and height
// Bitmap bmp = Bitmap. createBitmap (views. getDrawingCache (), 0, statusBarHeights, widths, heights-statusBarHeights );
Return bmp;
}
------------ Method of saving to SD card
[Java]
Try {
String status = Environment. getExternalStorageState ();
// Determine whether the SD card exists
If (status. equals (Environment. MEDIA_MOUNTED )){
File destDir = new File ("folder name ");
If (! DestDir. exists ()){
// Create a file producer
DestDir. mkdirs ();
}
File file = new File ("image name ");
// Determine whether the file metadata exists
If (file. exists ()){
String pic_path = "folder name" + "image name" + ". png ";
FileOutputStream out = new FileOutputStream (pic_path );
Shot (). compress (Bitmap. CompressFormat. PNG, 100, out );
Out. flush ();
Out. close ();
}
}
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
-------- Convert Bitmap to Drawable and put it into imageView
[Java]
// Bitmap --> Drawable
BitmapDrawable bd = new BitmapDrawable (shot ());
ImageView. setBackgroundDrawable (bd );
ImageView. setImageBitmap (shot ());