I need to take a screen shot and save the screen shot. I need to do this without using any connection to PC or without unrooting the phone. I need to do this whenever a event is triggered . For example when an ad is shown in a game ... or when the game ends and shows the scores in snake etc. Can you please let me know How Can i do this. I saw some tutorilas and they gave the code but that doesnt seem to work
private void getScreen()
{
View content = findViewById(R.id.layoutRoot);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/test.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
Can you give more information as to what does not work when you run that code ? Does it not capture what you want ? Does it crash ?
Make sure you change the
R.id.layoutroot
correctly with your root layout... Beside that it seems like it would work...Edit...
So for example, if you use that layout you just put there, you should change the
R.id.layout
intoR.id.snake
, that's because of this line :android:id="@+id/snake"
.I don't think there is an easy way to find /get the id of the "root" layout of a view (if you wanted to take screenshot of anything the phone is showing.
I just checked the launcher and another application, seems like most app are placed into a FrameLayout with id/content, so you can try to use
android.R.id.content
, but there is no guaranty this will work every time...You have to enable the cache first, before calling getDrawingCache().