Search code examples
androidscreenshot

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

Solution

  • 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