Trying to create a screenshot function

Monkey Targets Forums/Android/Trying to create a screenshot function

Aman(Posted 2012) [#1]
Hi all, it's been a long time since I did any posts. I guess I haven't done anything challenging in a long time. Anyway, I am trying to create a screenshot function for android. I am not sure what is wrong. Here is my code:
static boolean screenshot(boolean share)
{
  View myView =  MonkeyGame.view;
  myView.measure(bb_graphics.bb_graphics_DeviceWidth(), bb_graphics.bb_graphics_DeviceHeight());
  myView.layout(0, 0, bb_graphics.bb_graphics_DeviceWidth(), bb_graphics.bb_graphics_DeviceHeight());
  myView.setDrawingCacheEnabled(true);
  Bitmap myScreenshot= myView.getDrawingCache();
		
  //Save screenshot to a file
  File myFile = new File("screenshot.png");
  FileOutputStream fos;
  try {
	fos = new FileOutputStream(myFile);
	myScreenshot.compress(Bitmap.CompressFormat.PNG, 90, fos);
	fos.close();
  } catch (Exception e) {
	e.printStackTrace();
	return false;
  }
		
  //Share screenshot code goes here
  return true;
}

The bitmap is created with the correct width and height. I suspected that MonkeyGame.view give me an empty view. So I tried this:
Bitmap myScreenshot=MonkeyData.loadBitmap("graphics/logo.png");

It was still not saving the image to the file system. I tried saving a text file and loading it back and it worked fine. Any ideas?