DEV Community

Dhruv Joshi
Dhruv Joshi

Posted on


How to make a Screen Shot Android app? Step by step guide

To create an Android app that takes screenshots, you will need to use the Android SDK and programming language such as Java or Kotlin. Here is a step-by-step guide for creating a simple screenshot app:

  • Install the Android Studio development environment and set up an emulator or connect a physical Android device to your computer.

  • Create a new Android project in Android Studio.

  • Design the layout of your app. This will involve creating the necessary XML files to define the user interface of your app, including any buttons or other UI elements you want to include. For this example, we will create a simple layout with a single button to trigger the screenshot.

  • In your app's main activity, define a method to handle the screenshot button press. This method will use the Android MediaProjection class to capture a screenshot of the device's screen and save it to a file. Add the following code to your activity:

private void takeScreenshot() {
    // Create an instance of the MediaProjection class
    MediaProjection mediaProjection = ...;

    // Get the display metrics to determine the dimensions of the screenshot
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    int density = metrics.densityDpi;

    // Create a virtual display using the MediaProjection
    Surface surface = mediaProjection.createVirtualDisplay("Screenshot",
            width, height, density,
            null, null, null).getSurface();

    // Create a bitmap to hold the screenshot
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    // Create a canvas to draw the screenshot into the bitmap
    Canvas canvas = new Canvas(bitmap);

    // Draw the screenshot into the bitmap

    // Save the bitmap to a file
    File screenshotFile = new File(getExternalFilesDir(null), "screenshot.png");
    FileOutputStream fos = new FileOutputStream(screenshotFile);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

Enter fullscreen mode Exit fullscreen mode
  • In your layout XML file, add a button to trigger the screenshot. Set the button's onClick attribute to the name of the method you defined in step 4.

  • In your app's manifest file, add the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions to allow the app to save and read files from the device's storage.

  • Run your app on the emulator or physical device to test the screenshot functionality. When you press the button, the app should capture a screenshot and save it to the device's external storage.

I hope this helps! Let me know if you have any questions or need more guidance. You can reach me here.

Top comments (2)

taar1 profile image

WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE is deprecated. Also it would have been helpful if you actually created an instance of MediaProjection instead of just put a comment there.

dhruvjoshi9 profile image
Dhruv Joshi

Sure! Will do! :)