I need to take a screenshot which I was coding in my game result screen.
I used the following method that I found in StackOverflow:
public void TakeScreenShot(){
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
String mPath = Environment.getExternalStorageDirectory().toString() + "/PICTURES/Screenshots/" + now + ".jpg";
v1 = this.findViewById(android.R.id.content).getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
Toast.makeText(this, "Screenshot OK", Toast.LENGTH_SHORT).show();
} catch (Throwable e) {
e.printStackTrace();
}
}
but I'm getting NullPointerException
at Bitmap.CreateBitmap
.
My Bitmap and Views are global and I try to call method in OnCreate
.
11-04 15:20:17.868 13569-13569/com.finger W/System.err: java.lang.NullPointerException
How can I solve this problem?
I FOUND SOLUTION!
I try to call takeScreenshot method in onCreate and always i got same result all different variation, but i try call method in "Share Button" click event, now its working. Thank you.