I've seen the following Link and it does take a screenshot with the top answer
However, what I want is for the app to take a screenshot of the Alert Dialog that I am displaying to the user, the above solution and below code only takes a screenshot of what is currently behind the alert dialog and therefore no good
Here is the code being used in case anyone hasn't gone through the link provided
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap 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();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
EDIT: code for dialog as requested
public void showCalc(String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton("Capture + Open",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Remove Values From Inventory
captureScreenAndOpen();
}
});
builder.setNegativeButton("Capture",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
captureScreen();
Context context = getApplicationContext();
Toast.makeText(context, "Screenshot Captured", Toast.LENGTH_LONG).show();
}
});
builder.setNeutralButton("Return", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
FURTHER EDIT:
Here you will see two screenshots, the first is showing the saved screenshot when everything is saved in the screenshot from the dialog, you'll notice at the bottom there is a bit of text which is always present at the bottom.
The second screenshot is where there is so much text in the dialog the dialog is scrollable so that you can see all the data, you'll notice that the bottom string in the first screenshot is not present
If possible I'd like it that all the data is displayed, I'm not sure though if a screenshot function would be able to do this or an alternative method