Android screenshot and write it to the SD card

Source: Internet
Author: User

---------- 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 ());

 
 
 
 

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.