欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现屏幕截图并保存截图到指定文件

程序员文章站 2022-06-03 16:37:04
android屏幕截图并保存截取屏幕的图片到指定文件,具体内容如下 注意要添加权限:

android屏幕截图并保存截取屏幕的图片到指定文件,具体内容如下

注意要添加权限:

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

完整代码:

package zhangphil.test;
 
import android.app.activity;
import android.content.context;
import android.content.res.resources;
import android.graphics.bitmap;
import android.os.bundle;
import android.os.environment;
import android.support.annotation.nullable;
import android.support.v7.app.appcompatactivity;
import android.util.displaymetrics;
import android.view.view;
import android.widget.toast;
 
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.outputstream;
 
public class javaactivity extends appcompatactivity {
 
  @override
  protected void oncreate(@nullable bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.drawable_activity);
 
    appcompatactivity activity = this;
 
    findviewbyid(r.id.button).setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        file file = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures), "zhangphil.jpg");
        bitmap bitmap = screenshot(activity);
        try {
          if (!file.exists())
            file.createnewfile();
 
          boolean ret = save(bitmap, file, bitmap.compressformat.jpeg, true);
          if (ret) {
            toast.maketext(getapplicationcontext(), "截图已保持至 " + file.getabsolutepath(), toast.length_short).show();
          }
        } catch (exception e) {
          e.printstacktrace();
        }
      }
    });
  }
 
  /**
   * 保存图片到文件file。
   *
   * @param src   源图片
   * @param file  要保存到的文件
   * @param format 格式
   * @param recycle 是否回收
   * @return true 成功 false 失败
   */
  public static boolean save(bitmap src, file file, bitmap.compressformat format, boolean recycle) {
    if (isemptybitmap(src))
      return false;
 
    outputstream os;
    boolean ret = false;
    try {
      os = new bufferedoutputstream(new fileoutputstream(file));
      ret = src.compress(format, 100, os);
      if (recycle && !src.isrecycled())
        src.recycle();
    } catch (ioexception e) {
      e.printstacktrace();
    }
 
    return ret;
  }
 
 
  /**
   * 获取当前屏幕截图,不包含状态栏(status bar)。
   *
   * @param activity activity
   * @return bitmap
   */
  public static bitmap screenshot(activity activity) {
    view view = activity.getwindow().getdecorview();
    view.setdrawingcacheenabled(true);
    view.builddrawingcache();
    bitmap bmp = view.getdrawingcache();
    int statusbarheight = getstatusbarheight(activity);
    int width = (int) getdevicedisplaysize(activity)[0];
    int height = (int) getdevicedisplaysize(activity)[1];
 
    bitmap ret = bitmap.createbitmap(bmp, 0, statusbarheight, width, height - statusbarheight);
    view.destroydrawingcache();
 
    return ret;
  }
 
  public static float[] getdevicedisplaysize(context context) {
    resources resources = context.getresources();
    displaymetrics dm = resources.getdisplaymetrics();
    int width = dm.widthpixels;
    int height = dm.heightpixels;
 
    float[] size = new float[2];
    size[0] = width;
    size[1] = height;
 
    return size;
  }
 
  public static int getstatusbarheight(context context) {
    int height = 0;
    int resourceid = context.getresources().getidentifier("status_bar_height", "dimen", "android");
    if (resourceid > 0) {
      height = context.getresources().getdimensionpixelsize(resourceid);
    }
 
    return height;
  }
 
  /**
   * bitmap对象是否为空。
   */
  public static boolean isemptybitmap(bitmap src) {
    return src == null || src.getwidth() == 0 || src.getheight() == 0;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。