Android screenshot: Save the image content of a view and place it on the SD card.

Source: Internet
Author: User

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

}

}

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.