1

I am aware that not all Android devices can take screen shots, but I would like to be able to take screen shots of my users game screen programmatically so they can share it with friends etc. Kinda like Hill Climb racing does in there game.

Does anyone know how to do this? I tried a few solutions that are posted on here:

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

// create bitmap screen capture
Bitmap bitmap;
View v1 = activity.getCurrentFocus().getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
    fout.flush();
    fout.close();

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

But no luck. Any other way to do this? Maybe a new method.

EDIT:

I am getting a null pointer at line View v1 = activity.getCurrentFocus().getRootView();

2 Answers2

0

Try This In this I take screen shot of current layout and save in sdcard...

To Capture Screen Shoot

Community
  • 1
  • 1
Singhak
  • 8,508
  • 2
  • 31
  • 34
0

Use

View v1 = getWindow().getDecorView().getRootView();

in place of

View v1 = activity.getCurrentFocus().getRootView();

it will not throws Null Pointer Exception.

Enjoy!!

Abhishek Tamta
  • 463
  • 5
  • 14