How to programmatically take a screenshot in android of the Alert dialog box

I saw the following link and it takes a screenshot with the top answer

However, I want the application to take a screenshot of the Alert Dialog dialog box, which I show the user, the solution above and the code below only take a screenshot of what is currently behind the warning dialog, and therefore there is no good

Here is the code used if someone didn’t go through the provided link

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 on request

 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 EDITION:

Here you will see two screenshots, the first shows the saved screenshot, when everything is saved in the screenshot from the dialog box, you will notice that there is some text below that is always present below.

Screenshot1

In the second screenshot there is so much text in the dialog box that the dialogue scrolls so that you can see all the data, you will notice that the bottom line in the first screenshot is missing

Screenshot 2

If possible, I would like all the data to be displayed, I'm not sure if the screenshot function can do this or an alternative method

+5
source share
3 answers

Developed on Android 5 emulator and its work. We took the code of your code and the screenshot code from the link you provided.

This is your AlertDialog

 public void showCalc(String title, String message) { final 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) { AlertDialog dialog2 =AlertDialog.class.cast(dialog); takeScreenshot(dialog2); 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(); } 

This is a screenshot code.

 private void takeScreenshot(AlertDialog dialog) { 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 = "/data/data/com.rohit.test/test.jpg"; // use your desired path // create bitmap screen capture View v1 = dialog.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(); } catch (Throwable e) { // Several error may come out with file handling or OOM e.printStackTrace(); } } 

Screenshot

enter image description here

Note1:. You can generalize the takeScreenshot method if you change the type of the argument to View and move dialog.getWindow().getDecorView().getRootView(); into the code of the dialog from which this method is called.

Note2: We will see that you updated the question. I don’t think you can get the whole data in the screenshot when some of them are hidden. Think of it as a regular screenshot (on a computer or even on a phone). You photograph only what you can see.

+5
source

Try this library:

https://github.com/jraska/Falcon

It can capture dialogs in a screenshot.

+2
source

1. My dear friend. You are doing one thing that’s wrong, for which you cannot take a screenshot of the dialog box.

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

You capture the entire screen below your AlertDialog

You can use these methods to achieve your goals by sending a view of this dialog box to this method.

Get bitmap from view

  public static Bitmap loadView(View v) { Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.layout(0, 0, v.getWidth(), v.getHeight()); v.draw(c); return b; } 

Saving a Bitmap

  void saveFile(Bitmap bitmap) { String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "Folder"; String fileName = new SimpleDateFormat("yyyyMMddhhmm'_bitmap.jpg'", Locale.US).format(new Date()); File myPath = new File(extr, fileName); FileOutputStream fos = null; try { fos = new FileOutputStream(myPath); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Screen", "screen"); } catch (Exception e) { e.printStackTrace(); } } 
0
source

Source: https://habr.com/ru/post/1243123/


All Articles