Android takes a long screenshot of scrollView beyond the screen size to form a long image.

Source: Internet
Author: User

Android generates a long image for scrollView exceeding the screen size

Many times, we want to share all the content of an interface, but there is too much content, beyond the size of the screen, simple screenshots can no longer meet our needs, at this time, we can capture the Image Based on the height of the scrollView in the layout.

The Code is as follows:

/*** Capture the scrollview screen * @ param scrollView * @ return */public static Bitmap getBitmapByView (ScrollView scrollView) {int h = 0; Bitmap bitmap = null; // obtain the actual height of the scrollview for (int I = 0; I <scrollView. getChildCount (); I ++) {h + = scrollView. getChildAt (I ). getHeight (); scrollView. getChildAt (I ). setBackgroundColor (Color. parseColor ("# ffffff");} // create a bitmapbitmap = Bitmap. createBitmap (scrollView. getWidth (), H, Bitmap. config. RGB_565); final Canvas canvas = new Canvas (bitmap); scrollView. draw (canvas); return bitmap;}/*** compressed image * @ param image * @ return */public static Bitmap compressImage (Bitmap image) {ByteArrayOutputStream baos = new ByteArrayOutputStream (); // quality compression method. Here, 100 indicates no compression. The compressed data is stored in the baos image. compress (Bitmap. compressFormat. JPEG, 100, baos); int options = 100; // cyclically determine whether the size of the image after compression is greater than kb. S. toByteArray (). length/1024> 100) {// reset baosbaos. reset (); // here, compress options % and store the compressed data in baos image. compress (Bitmap. compressFormat. JPEG, options, baos); // reduce each time by 10 options-= 10;} // Save the compressed data baos to ByteArrayInputStream isBm = new ByteArrayInputStream (baos. toByteArray (); // generate ByteArrayInputStream data to the image Bitmap bitmap = BitmapFactory. decodeStream (isBm, null, null); return bitmap;}/*** save Sdcard * @ param B * @ return */public static String savePic (Bitmap B) {SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd_HH-mm-ss", Locale. US); File outfile = new File ("/sdcard/image"); // if the File does not exist, create a new File if (! Outfile. isDirectory () {try {outfile. mkdir ();} catch (Exception e) {e. printStackTrace () ;}} String fname = outfile + "/" + sdf. format (new Date () + ". png "; FileOutputStream fos = null; try {fos = new FileOutputStream (fname); if (null! = Fos) {B. compress (Bitmap. compressFormat. PNG, 90, fos); fos. flush (); fos. close () ;}} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return fname ;}


Call the getBitmapByView () method as needed:

String fname = ScreenShot.savePic(ScreenShot.getBitmapByView(scrollView));

However, in this case, the outofmemory error is reported because the captured image is too long. To avoid memory overflow, you must use Config. RGB_565, will occupy less memory than ARGB_8888. Also, compress the image. At least I will not report the oom error, that is:
String fname = ScreenShot.savePic(ScreenShot.compressImage(ScreenShot.getBitmapByView(scrollView)));


Related Article

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.