I am working on android. I have following code to capture a screenshot.
Setting up your Root layout:
View content = findViewById(R.id.layoutroot);
content.setDrawingCacheEnabled(true);
Function to get the rendered view:
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开发者_开发百科();
}
}
My problem is that it creates a zero kb file. what could be problem?
do these changes and try again:
make the content variable a final and call findViewById on the layout of the activity
final View content = somelayout.findViewById(R.id.layoutroot);
You have not write data to the file. you are directly closing the file thats why you got a file with 0 kb
use something like this
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
byte[] mydata = null;//data in byte array
ostream.write(mydata);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
精彩评论