Android New posture: Screenshot code Grooming

Source: Internet
Author: User
Tags root access

Today to do the project to use the Android screenshot function, at first I was glad to read some blog articles, self-confidence can easily solve ... --the result was a day before the end of a similar ... Hey!

About Android screenshot of the code, there are roughly 3 ways, interested in watching it.

Method One:

Read a lot of articles on the Internet, mostly using this method, directly convert a view into bitmap, and then save to the SD card.

/**

* Generate bitmap images based on view, available for function

 */

public static Bitmap Getviewbitmap (View v) {

V.clearfocus (); //

v.setpressed (false); //

//can draw the cache to return false

Boolean willnotcache = V.willnotcachedrawing ();

v.setwillnotcachedrawing (false);

int color = V.getdrawingcachebackgroundcolor ();

v.setdrawingcachebackgroundcolor (0);

if (color! = 0) {

V.destroydrawingcache ();

}

V.builddrawingcache ();

Bitmap cachebitmap = V.getdrawingcache ();

if (Cachebitmap = = null) {

return null;

}

Bitmap Bitmap = Bitmap.createbitmap (Cachebitmap);

//Restore The View

V.destroydrawingcache ();

v.setwillnotcachedrawing (Willnotcache);

v.setdrawingcachebackgroundcolor (color);

return bitmap;

}

/**

* Save bitmap picture as local file

 */

public static void SaveFile (Bitmap Bitmap, String filename) {

FileOutputStream fileoutputstream = null;

try {

fileoutputstream = new FileOutputStream (filename);

if (fileoutputstream! = null) {

bitmap.compress (Bitmap.CompressFormat.PNG, FileOutputStream);

Fileoutputstream.flush ();

fileoutputstream.close ();

}

} catch (FileNotFoundException e) {

l.d ("exception:filenotfoundexception");

e.printstacktrace ();

} catch (IOException e) {

l.d ("ioexception:ioexception");

e.printstacktrace ();

}

}

This method is simple to use, SaveFile (Getviewbitmap (view), filename), and the code is done.

But in the project to use after the pit father's discovery WebView can't intercept the diagram!! Qaq all right, had to look for information everywhere.

Baidu Google found such a code:

/**

* Intercept the WebView visible area

* @param webView premise: WebView to set webview.setdrawingcacheenabled (true);

* @return

 */

public static Bitmap capturewebviewvisiblesize (WebView WebView) {

Bitmap bmp = Webview.getdrawingcache ();

return bmp;

}

/**

* Intercept WebView Snapshot (the size of the entire content loaded by WebView)

* @param webView

* @return

 */

public static Bitmap Capturewebview (WebView WebView) {

Picture snapShot = Webview.capturepicture ();

Bitmap bmp = Bitmap.createbitmap (Snapshot.getwidth (),

snapshot.getheight (), Bitmap.Config.ARGB_8888);

Canvas canvas = new canvas (BMP);

Snapshot.draw (canvas);

return bmp;

}

But still can not solve my problem, cause I think it should be my webview is to use the Anychart JS file and SWF file to generate the chart, the web also found some people like me, said to use the SWF page can not.

Method Two:

Intercepts the view of the current activity and saves it. This can be said to be more simple than method one, but not flexible enough, because the cut figure is the entire screen, but I do not want the status bar and the title bar. Qaq

But if it succeeds, it's okay to cut out the cut to get the part you need. Unfortunately, this method cannot cut out webview dynamic graphs.

The code is given below:

/**

* screenshot

* @param activity

* @return

 */

public static Bitmap Capturescreen (activity activity) {

Activity.getwindow (). Getdecorview (). Setdrawingcacheenabled (True);

Bitmap Bmp=getwindow (). Getdecorview (). Getdrawingcache ();

return bmp;

}

In fact, this method is also a method of extension, are used to view the Getdrawingcache () method to obtain bitmap, so it is not a matter of course.

Method Three:

The third method is to use the framebuffer implementation of the screenshot, this is the real sense! (Alas, overtime for 1 hours, finally let me find some clues!) )

The first introduction to the next Framebuffer:framebuffer is the Linux kernel to display the lowest level of driver. In a generic Linux file system, the/DEV/FB0 device file is provided to the application for read-write access to the framebuffer. Here, if you have more than one display device, the FB1,FB2 will appear sequentially,... and other documents. And in what we call Android, this device file is put in/dev/graphics/fb0, and it's often the only one.

(Do you understand?) I can't read it anyway ... )

As for the detailed principle of interest can go to Baidu "framebuffer to get Android screen"

The following said I compiled the code, tested on my mobile phone can be successful, the use of Anychart WebView can also be truncated.

/**

* screenshot

* @param activity

* @return

 */

public static Bitmap Capturescreen (activity activity) {

//Get screen Size:

displaymetrics metrics = new Displaymetrics ();

WindowManager WM = (windowmanager) activity

. Getsystemservice (Context.window_service);

Display display = Wm.getdefaultdisplay ();

display.getmetrics (metrics);

int height = metrics.heightpixels; Screen height

int width = metrics.widthpixels; The width of the screen

//Get display mode

int pixelformat = Display.getpixelformat ();

PixelFormat localPixelFormat1 = new PixelFormat ();

pixelformat.getpixelformatinfo (PixelFormat, LOCALPIXELFORMAT1);

int deepth = localpixelformat1.bytesperpixel;//bit Depth

byte[] Piex = new Byte[height * width * deepth];

try {

runtime.getruntime (). EXEC (

New string[] {"/system/bin/su", "-C",

"chmod 777/dev/graphics/fb0"});

} catch (IOException e) {

e.printstacktrace ();

}

try {

//Get fb0 data input stream

InputStream stream = new FileInputStream (New File (

"/dev/graphics/fb0"));

DataInputStream dStream = new DataInputStream (stream);

dstream.readfully (PIEX);

} catch (Exception e) {

e.printstacktrace ();

}

//Save Picture

int[] colors = new int[height * Width];

for (int m = 0; m < colors.length; m++) {

int r = (PIEX[M * 4] & 0xFF);

int g = (PIEX[M * 4 + 1] & 0xFF);

int b = (Piex[m * 4 + 2] & 0xFF);

int a = (PIEX[M * 4 + 3] & 0xFF);

Colors[m] = (a <<) + (R << +) + (g << 8) + B;

}

//Piex generate bitmap

Bitmap Bitmap = bitmap.createbitmap (colors, width, height,

Bitmap.Config.ARGB_8888);

return bitmap;

}

Remember to add two lines of permissions in Androidmanifest.xml:

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name= "Android.permission.READ_FRAME_BUFFER"/>

There is also a need to note that the phone needs root access, not the root of the mobile phone I have not tried, but the estimate is not possible.

So the most of the day, or not satisfied! Not everyone's phone has root privileges ... Qaq but I have done my best, let's do it first! Then it will improve.


Android New posture: Screenshot code Grooming

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.