Noman
Noman

Reputation: 4109

Screenshot is black

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();
}

Upvotes: 0

Views: 3348

Answers (3)

user1025013
user1025013

Reputation: 123

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 :)

Upvotes: 0

Krishnabhadra
Krishnabhadra

Reputation: 34296

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..

Upvotes: 2

user370305
user370305

Reputation: 109257

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

Upvotes: 1

Related Questions