2

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.

Community
  • 1
  • 1
Srini
  • 21
  • 1
  • 1
  • 2
  • What should the device be doing with this screenshot? Isn't there already a screenshot function built into all android phones? If so does this broadcast some sort of intent? If it does could you not just use the phones feature and wait for the broadcasted intent? – Jon Taylor Jul 11 '12 at 10:18
  • Nevermind I just looked it up and it does not broadcast an intent. – Jon Taylor Jul 11 '12 at 10:20
  • http://code.google.com/p/android-screenshot-library/wiki/Usage try looking here this may help. – Jon Taylor Jul 11 '12 at 10:21
  • As long as you don't use this for dodgy things... – darryn.ten Jul 11 '12 at 10:51

1 Answers1

9

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.

Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • Hi Thanks for reply! I want to capture the device screen not my activity , the idea is if user opened any of the image file and press menu button then I want to capture screen! – Srini Jul 12 '12 at 06:22
  • Yea I understand you. For that you will have to get the root layout of the screen. Get the root layout of your activity. – Antrromet Jul 12 '12 at 09:21
  • could you please paste me the code to get the root layout , still I have doubt that if my activity is launched and get the root layout means it will get the back ground window screen instead of my current activity screen? sorry for asking his doubt! – Srini Jul 13 '12 at 07:33
  • @Srini Check out this link http://stackoverflow.com/questions/4486034/android-how-to-get-root-view-from-current-activity – Antrromet Jul 13 '12 at 09:09