1.1 First, let’s look at your screen capture. This code is flawed and can only be captured once.
getWindow().getDecorView().setDrawingCacheEnabled(true); Bitmap screenBitmap = getWindow().getDecorView().getDrawingCache(); img_display.setImageBitmap(screenBitmap);
1.2 The following can be intercepted every time (only the visible screen part can be intercepted, the invisible part cannot be intercepted)
View decorView = getWindow().getDecorView(); Bitmap screenBitmap = Bitmap.createBitmap(decorView.getWidth(), decorView.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(screenBitmap); decorView.draw(canvas);
1.3 Intercept visible and invisible screen parts (except ListView and GridView, only ScrollView and HorizontalScrollView can be intercepted), the reason is that the adaptation mechanism of ListView and GridView is constantly remove and add
Note: The screenshot here is the View instead of the screen
ScrollView and HorizontalScrollView must have only one sub-layout, that is, his sub-layout is used as a container, and it is used as a scroll control
View decorView = getWindow().getDecorView(); ScrollView sv = (ScrollView)findViewById(R.id.scrollbox); LinearLayout panel= (LinearLayout)sv.findViewById(R.id.scrollbox_panel); int sumHeight = 0; for(int i=0;i<panel.getChildCount();i++) { sumHeight += panel.getChildAt(i).getHeight(); } Bitmap bmp = Bitmap.createBitmap(panel.getWidth(),sumHeight,Config.ARGB_8888); Canvas canvas = new Canvas(bmp); decorView.draw(panel);
The picture is saved using the interface provided by ContentProvider, and the following is the Uri positioning of the album
Images.Media.EXTERNAL_CONTENT_URI
Easiest way to save
//The return value is the Uri protocol string String uriString = MediaStore.Images.Media.insertImage(context.getContentResolver(), bmp, "screenshot-20141121", "this is my screenshot");
/** //Save to a path File dir = new File("/sdcard/t/"); if (!dir.exists()) { dir.mkdirs(); } final String photoUrl = "/sdcard/t/" + System.currentTimeMillis() + ".png";//Replace with your own image save path final File file = new File(photoUrl); try { FileOutputStream out = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace (); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace (); } **/