1

I'm trying to create an app which allows users to screenshot the current view. I'm using the following code to do so.

View content = ((Activity)ctx).findViewById(R.id.rootlayout);
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();
}

However, i'm getting a Null Pointer Exception on the following line:

Bitmap bitmap = content.getDrawingCache();

What would this mean, that the view is empty?

Any help would be great!

1 Answer 1

2

You need to call content.setDrawingCacheEnabled(true); before you can use that. Then call content.setDrawingCacheEnabled(false); when you're done.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.