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