Android截图并分享的小例子

来源:互联网 发布:数据分析属于什么职能 编辑:程序博客网 时间:2024/06/18 17:27

创建ScreenShot类来进行截图处理,并返回保存的文件名

public class ScreenShot {// 获取指定Activity的截屏,保存到png文件    private static Bitmap takeScreenShot(Activity activity) {        // View是你需要截图的View        View view = activity.getWindow().getDecorView();        view.setDrawingCacheEnabled(true);        view.buildDrawingCache();        Bitmap b1 = view.getDrawingCache();        // 获取状态栏高度        Rect frame = new Rect();        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);        int statusBarHeight = frame.top;        Log.i("TAG", "" + statusBarHeight);        // 获取屏幕长和高        int width = activity.getWindowManager().getDefaultDisplay().getWidth();        int height = activity.getWindowManager().getDefaultDisplay()                .getHeight();        // 去掉标题栏        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height                - statusBarHeight);        view.destroyDrawingCache();        return b;    }    // 保存到sdcard    private static void savePic(Bitmap b, String strFileName) {            FileOutputStream fos = null;        try {            fos = new FileOutputStream(strFileName);            if (null != fos) {                b.compress(Bitmap.CompressFormat.PNG, 90, fos);                fos.flush();                fos.close();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    // 程序入口    public static String shoot(Activity a) {    String strFileName = "sdcard/" + String.valueOf(System.currentTimeMillis()) + ".png";        ScreenShot.savePic(ScreenShot.takeScreenShot(a), strFileName);        return strFileName;    }}

在需要使用的Activity中调用shoot方法,并使用Intent来分享出去

public class MainActivity extends Activity {private Button btn;private String image;private Button btnShare;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn = (Button)findViewById(R.id.btn);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {image = ScreenShot.shoot(MainActivity.this);}});btnShare = (Button) findViewById(R.id.btn_share);btnShare.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent  = new Intent(Intent.ACTION_SEND);File file = new File(image);intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));intent.setType("image/jpeg");Intent chooser = Intent.createChooser(intent, "Share screen shot");if(intent.resolveActivity(getPackageManager()) != null){startActivity(chooser);}}});}}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 dnf脸黑怎么办还有办法 我想登别人微信怎么办 昌硕工资没到怎么办 昌硕离职不批怎么办 昌硕工资不到卡怎么办 上海人去苏州工作社保怎么办 娶个个脾气暴躁的媳妇怎么办 满脸的黑头痘痘怎么办 脚底磨起泡不敢走路怎么办 老婆老是找异性朋友怎么办 喜欢的人有对象怎么办 遇到了更喜欢的怎么办 8岁骨龄11岁怎么办啊 13岁初中生有思想不听话怎么办 交朋友找对象喜欢颜值高的怎么办 儿子找对象不听父母怎么办 缺爱怎么办的搞笑回答 对象说有人追她怎么办 说了一句话媳妇非常生气怎么办 柔顺后头发太臭怎么办 积分兑换手机被骗了怎么办 老板对你的上级不满怎么办 如果老板不给工资怎么办 手机号码被标记为其他公司怎么办? 被标记为骚扰电话怎么办 手机被标记骚扰电话怎么办 360摄像头不支持5g怎么办 摄像头不支持5g网络怎么办 家里的wifi卡了怎么办 办信用卡没有座机号码怎么办 拨打电话时显示号码有误怎么办 个体营业执照怎么办企业支付宝 没满16岁怎么办电话卡 我的电话卡丢了怎么办 公司注销地税没有补齐怎么办 公司没有固定电话怎么办信用卡 申请信用卡公司没有固定电话怎么办 办理信用卡公司没有固定电话怎么办 江门市固话欠费停机后怎么办 物业交钥匙地面有裂缝怎么办 深圳交通卡丢了怎么办