1

I have Android application which interacts with Web Server. When data is sent from phone to server, there is a bit of work with data (10-20 secs) and after that I get result back to phone. And i display that result in dialog with 2 choices. One choice is "OK" and it means that user is fine with result and dismiss the dialog. The other choice makes a screenshot of that results. And when I press this button I don't get picture of dialog, i get picture of layout which is behind that dialog.

enter image description here

My code behind this is

//this method is called on onPostExecute of async task

public void recieveResult (final Activity act,String result){
    new AlertDialog.Builder(act)
            .setTitle("Data - status")
            .setMessage(result)
            .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            })
            .setPositiveButton("Save picture", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Bitmap bitmap = takeScreenshot(act);
                    saveBitmap(bitmap,act);
                    Toast.makeText(act,"Saved",Toast.LENGTH_LONG).show();
                    dialog.dismiss();
                }
            })
            .show();
}

public Bitmap takeScreenshot(Activity act) {
    View rootView = act.findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap,Activity act) {
    String timeStamp = new SimpleDateFormat("MMyyyydd_HHmmss").format(new Date());
    String imageFileName = "/screenshot_"+timeStamp+"_.png";
    File storage = Environment.getExternalStoragePublicDirectory("Screens");
    if (!storage.exists())
    {
        Toast.makeText(act, "Directory made", Toast.LENGTH_LONG).show();
        storage.mkdirs();
    }
    File imagePath = new File(storage.getPath() + imageFileName);
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

My result of picture is just screen without dialog. How to get picture of dialog in it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
KuKeC
  • 4,392
  • 5
  • 31
  • 60

3 Answers3

0

Try obtaining the Windows's decor view

then get its drawing cache bitmap as you implemented.

Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
0

Dialog has it's own window ( different form activity's window ), you can use dialog.getWindow() method to get dialog's window.

so here is the screenshot code:

private AlertDialog dialog;

public void showDialog(View view) {
    dialog = new AlertDialog.Builder(this)
            .setTitle("Titel")
            .setMessage("some content")
            .setPositiveButton("Screenshot", (d, which) -> {
                Bitmap bitmap = screenshot(dialog);
                // you can save the bitmap or display in an ImageView
            }).create();
    dialog.show();
}

private Bitmap screenshot(Dialog dialog) {
    Window window = dialog.getWindow();
    View decorView = window.getDecorView();
    Bitmap bitmap = Bitmap.createBitmap(decorView.getWidth(), decorView.getHeight(), Bitmap.Config.ARGB_8888);
    decorView.draw(new Canvas(bitmap));
    return bitmap;
}
-1

try this code

 String picId=String.valueOf(nu);
                    String myfile="meter"+picId+".jpeg";

                    BitmapDrawable bitmapDrawable = null;
                    Toast.makeText(getActivity(),"success full store image gallery",Toast.LENGTH_SHORT).show();
                    Date now = new Date();
                    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

                    try {

                        File dir_image = new  File(Environment.getExternalStorageDirectory()+//<---
                                File.separator+"SoundMeter");  //23-1-16          //<---
                        dir_image.mkdirs();

                        // 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 = surfaceView.getRootView();
                        v1.setDrawingCacheEnabled(true);
                        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());

                       /* bitmapDrawable = new BitmapDrawable(bitmap);*/
                        v1.setDrawingCacheEnabled(false);
                       // File imageFile = new File(mPath);     //file path 23-1-16
                        File imageFile = new File(dir_image,myfile);

                        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();
                    }
                    btnCaptured1.setVisibility(View.VISIBLE);
                    btnCaptured.setVisibility(View.VISIBLE);
                    //btnCaptured.setBackgroundDrawable(bitmapDrawable);

                }
            });

        }
        else
        {
            Toast.makeText(getActivity().getApplicationContext(), "No Connection", Toast.LENGTH_LONG).show();

        }
droidev
  • 7,352
  • 11
  • 62
  • 94
Piyush Amipara
  • 135
  • 1
  • 12