SoFunction
Updated on 2025-04-09

Android implements screenshots and saves screenshots to specified files

Android screenshot and save the screenshot picture to the specified file. The specific content is as follows

Note that you want to add permissions:

<uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />

Complete code:

package ;
 
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .;
import ;
import ;
import ;
 
import ;
import ;
import ;
import ;
import ;
 
public class JavaActivity extends AppCompatActivity {
 
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.drawable_activity);
 
    AppCompatActivity activity = this;
 
    findViewById().setOnClickListener(new () {
      @Override
      public void onClick(View v) {
        File file = new File((Environment.DIRECTORY_PICTURES), "");
        Bitmap bitmap = screenShot(activity);
        try {
          if (!())
            ();
 
          boolean ret = save(bitmap, file, , true);
          if (ret) {
            (getApplicationContext(), "Screenshots are maintained to" + (), Toast.LENGTH_SHORT).show();
          }
        } catch (Exception e) {
          ();
        }
      }
    });
  }
 
  /**
    * Save the picture to file file.
    *
    * @param src Source image
    * @param file file to save to
    * @param format
    * @param recycle Whether to recycle
    * @return true Success false Failed
    */
  public static boolean save(Bitmap src, File file,  format, boolean recycle) {
    if (isEmptyBitmap(src))
      return false;
 
    OutputStream os;
    boolean ret = false;
    try {
      os = new BufferedOutputStream(new FileOutputStream(file));
      ret = (format, 100, os);
      if (recycle &amp;&amp; !())
        ();
    } catch (IOException e) {
      ();
    }
 
    return ret;
  }
 
 
  /**
    * Get the current screenshot, not including the Status Bar.
    *
    * @param activity activity
    * @return Bitmap
    */
  public static Bitmap screenShot(Activity activity) {
    View view = ().getDecorView();
    (true);
    ();
    Bitmap bmp = ();
    int statusBarHeight = getStatusBarHeight(activity);
    int width = (int) getDeviceDisplaySize(activity)[0];
    int height = (int) getDeviceDisplaySize(activity)[1];
 
    Bitmap ret = (bmp, 0, statusBarHeight, width, height - statusBarHeight);
    ();
 
    return ret;
  }
 
  public static float[] getDeviceDisplaySize(Context context) {
    Resources resources = ();
    DisplayMetrics dm = ();
    int width = ;
    int height = ;
 
    float[] size = new float[2];
    size[0] = width;
    size[1] = height;
 
    return size;
  }
 
  public static int getStatusBarHeight(Context context) {
    int height = 0;
    int resourceId = ().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId &gt; 0) {
      height = ().getDimensionPixelSize(resourceId);
    }
 
    return height;
  }
 
  /**
    * Whether the Bitmap object is empty.
    */
  public static boolean isEmptyBitmap(Bitmap src) {
    return src == null || () == 0 || () == 0;
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.