bylynka
bylynka

Reputation: 202

Take screenshot with dialog

I need to take screenshot programmatically of activity view. I have found a lot of answers how to do it, but there is not answer how to do it with opened dialog

Upvotes: 13

Views: 5214

Answers (4)

Majid ff
Majid ff

Reputation: 249

This is work for me (but without dialog):

public class MainActivity extends AppCompatActivity {

private View main;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Here we Checking CALL_PHONE permission

    setContentView(R.layout.activity_main);
    main = findViewById(R.id.main);
    imageView = (ImageView) findViewById(R.id.imageView);
    Button btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {

            Bitmap b = Screenshot.takescreenshotOfRoot(imageView);
            imageView.setImageBitmap(b);
            main.setBackgroundColor(Color.parseColor("#999999"));
        }
    });

}

}

Screenshot class:

public class Screenshot {

public static Bitmap takescreenshot (View v) {
    v.setDrawingCacheEnabled(true);
    v.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);
    return b;
}

public static Bitmap takescreenshotOfRoot(View v) {
    return takescreenshot(v.getRootView());

}

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.balance.MainActivity">

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Take Screenshot"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.055" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="355dp"
    android:layout_height="485dp"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@android:drawable/menuitem_background"
    tools:layout_editor_absoluteX="17dp" />


  </android.support.constraint.ConstraintLayout>    

Upvotes: 0

Bhavesh Vadalia
Bhavesh Vadalia

Reputation: 814

I have code for capture screen programmatically..

hope this code help you..

public class CaptureScreenShots extends Activity {
LinearLayout L1;
ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen_shots);
     L1 = (LinearLayout) findViewById(R.id.LinearLayout01);
        Button but = (Button) findViewById(R.id.munchscreen);
        but.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                View v1 = L1.getRootView();
                v1.setDrawingCacheEnabled(true);
                Bitmap bm = v1.getDrawingCache();
                BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
                image = (ImageView) findViewById(R.id.screenshots);
                image.setBackgroundDrawable(bitmapDrawable);
            }
        });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.screen_shots, menu);
    return true;
}

}

below are layout file

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/LinearLayout01"
    >
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/munch"
    android:id="@+id/munchscreen"
    />
  <ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/screenshots"
    android:contentDescription="@string/app_name"
    />

</LinearLayout>

Upvotes: 1

Ken Wolf
Ken Wolf

Reputation: 23269

Use the "screen capture" button in the "Devices" window of DDMS with a device/emulator connected. This allows you to take screenshots of whatever is on screen, including when a dialog is open.

enter image description here

EDIT: when I answered this question, it was not clear it needed to be done programmatically and was later edited to clarify.

Upvotes: 0

Aswin Rajendiran
Aswin Rajendiran

Reputation: 3409

Ok this one is a little tricky. There is no direct way to do it as far as I know. Here is something that works. I tried to take individual shots and layer them.

Dialog dialog = // Dialog that is showing

View mainView = getSupportActivity().getWindow().getDecorView();
mainView = getSupportActivity().getWindow().getDecorView()
        .findViewById(android.R.id.content);
mainView.setDrawingCacheEnabled(true);
// This is the bitmap for the main activity
Bitmap bitmap = mainView.getDrawingCache();

View dialogView = dialog.getView();
int location[] = new int[2];
mainView.getLocationOnScreen(location);
int location2[] = new int[2];
dialogView.getLocationOnScreen(location2);

dialogView.setDrawingCacheEnabled(true);
// This is the bitmap for the dialog view
Bitmap bitmap2 = dialogView.getDrawingCache();

Canvas canvas = new Canvas(bitmap);
// Need to draw the dialogView into the right position
canvas.drawBitmap(bitmap2, location2[0] - location[0], location2[1] - location[1],
        new Paint());

String filename = // filename to save
File myPath = new File(filename);
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(myPath);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();
    Trace.d("Twitter", "The file path is " + myPath);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I just tried this and it worked. Hope this helps. Let me know if it doesn't work.

Upvotes: 8

Related Questions