0

I'm trying to take a screen shot and save it as a png file on the sdcard. My file is saved with its file size as 1.57Kb but it is black. I'm using the following code:

View content = findViewById(R.id.id_ll_SurfaceView);
content.setDrawingCacheEnabled(true);
Bitmap b = content.getDrawingCache();
b.createBitmap(800, 480, Config.ARGB_8888);
File file = new File("/sdcard/test.png");
try
{
    file.createNewFile();
    FileOutputStream fos = new FileOutputStream(file);
    FileOutputStream(
        Environment.getExternalStorageDirectory().getAbsoluteFile()+"/test.jpg"));
    b.compress(CompressFormat.PNG, 100, fos);
    fos.close();
    Toast.makeText(getApplicationContext(), "Saved", 0).show();
}
catch (Exception e)
{
    e.printStackTrace();
}
Tobbe
  • 1,825
  • 3
  • 21
  • 37
Noman
  • 4,049
  • 10
  • 38
  • 59
  • Where are you calling the code? Is the view `id_ll_SurfaceView` already rendered on screen which this code is executed? – Karthik Dec 09 '11 at 06:50
  • you can also check [here](http://stackoverflow.com/questions/8294110/taking-screenshot/8366223#8366223). – Lalit Poptani Dec 09 '11 at 07:00
  • `b.createBitmap(800, 480, Config.ARGB_8888);` line is completely useless...`createBitmap` is static funtion, you're ignoring the returned Bitmap. I think you mean to resize the Bitmap. – st0le Dec 09 '11 at 07:33
  • actually i m using cocos2d.... now i placed some images on its surfaceview, played aroung a bit and trying to save the whole screen as image file...this is the full scenerio – Noman Dec 09 '11 at 07:34
  • work... on... accept... rate! – Anthony Graglia Jan 03 '12 at 13:42

3 Answers3

2

Are you taking screenshot from onCreate()..You have to wait till view is completely drawn on the screen..Use ViewTreeObserver to get a callback when view is completely drawn on the screen..

add this code in onCreate..

ViewTreeObserver vto = yourView.getViewTreeObserver();

vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  @Override
  public void onGlobalLayout() {
    layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    yourScreenshotFunction();
  }
});

Now implement this function

public void yourScreenshotFunction(){
 //Add your screenshot taking code here..
}

See my question here..I have explained the procedure under title Final Outcome..

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
1

I am doing just,

main.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(main.getDrawingCache());
File file = new File("/sdcard/test.png");
        try
        {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            screenshot.compress(CompressFormat.PNG, 100, fos);
            fos.close();
            Toast.makeText(getApplicationContext(), "Saved", 0).show();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }   

Here main is your activity's view..

EDIT: Look at this tutorial Android take screenshot from code

user370305
  • 108,599
  • 23
  • 164
  • 151
  • @user370305 Can you please copy your main activity aswell? Not sure how to include getDrawingcache and serDrawingCacheEnabled() functions. Thank you – Sandeep May 02 '13 at 03:31
0

Go to eclipse...

  1. Go to Windows
  2. Open Perspective
  3. DDMS
  4. Select your emulator or phone on the left hand side.
  5. Click on the camera icon just above in the toolbar.
  6. EASY AS THAT :)
user1025013
  • 123
  • 1
  • 9
  • @user1025013... user will not use Eclipse in his phone for the application! – Noman Dec 19 '11 at 07:39
  • @Noman Yeah he said he just wants a screenshot of the phone not an app to do it... he wants to create an app that does it but this shows him something that is already done – user1025013 Dec 19 '11 at 07:56