Capture Screen shot on Android using Java code [du

2019-01-15 17:28发布

问题:

Possible Duplicate:
Screenshot Android

I would like to capture screenshots programatically (not the screen shot of my view) and have created an application to take the screen shot of my view.

I am trying to capture the device screen shot (like if user opned any image file) by using Java code(I thought of creating service and from service we can take picture but I am not sure how to achieve this).

I found some native code using frame buffer fb0 but I am very knew to this and no idea how to use it.

回答1:

Here is a solution to take the screenshot of your screen and write it to the sd card

Setting up your Root layout:

View content = findViewById(R.id.layoutroot);
content.setDrawingCacheEnabled(true);

Function to get the rendered view:

private void getScreen()
{
    View content = findViewById(R.id.layoutroot);
    Bitmap bitmap = content.getDrawingCache();
    File file = new File( Environment.getExternalStorageDirectory() + "/test.png");
    try 
    {
        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, ostream);
        ostream.close();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

Remember to add

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

to your AndroidManifest.