Android screenshot: Save the image content of a view and place it on the SD card.
Screenshot sharing is occasionally used in the project, so the following screenshot method is available ~
The saveImage () method is used to save screenshots of all the screen content corresponding to the current Activity.
Private void saveImage (){
// SD card storage path
String savePath = Environment. getExternalStorageDirectory () + "/temp.png ";
// ShowProgress ("Please wait", "saving the image ...... ");
SaveMyBitmap (getBitmapFromRootView (getWindow (). getDecorView (), savePath );
}
// Obtain the view and convert it to a bitmap image
Private static Bitmap getBitmapFromRootView (View view ){
View. setDrawingCacheEnabled (true );
Bitmap bmp = Bitmap. createBitmap (view. getDrawingCache ());
View. setDrawingCacheEnabled (false );
If (bmp! = Null ){
Return bmp;
} Else {
Return null;
}
}
// Save the bitmao image to the corresponding SD card path
Private void saveMyBitmap (Bitmap mBitmap, String path ){
File f = new File (path );
Try {
F. createNewFile ();
} Catch (IOException e ){
E. printStackTrace ();
}
FileOutputStream fOut = null;
Try {
FOut = new FileOutputStream (f );
} Catch (FileNotFoundException e ){
E. printStackTrace ();
}
If (mBitmap! = Null ){
// Save the PNG quality as 100
MBitmap. compress (Bitmap. CompressFormat. PNG, 100, fOut );
}
Try {
FOut. flush ();
} Catch (IOException e ){
E. printStackTrace ();
}
Try {
FOut. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}