先给大家展示下效果图吧
直接上代码:
xml的布局:
<button android:id="@+id/btn_jp" android:layout_margintop="10dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="截屏" android:textcolor="#ff999999" /> <button android:id="@+id/btn_share" android:layout_margintop="10dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="分享" android:textcolor="#ff999999" />
activity的方法:
private string imagepath; //截屏 btnjp.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // image = screenshot.shoot(addressselecteractivity.this); screenshot(); // bitmap bitmap = getbitmapbyview(scrollview); // savepic(bitmap); } }); //分享 btnshare.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (imagepath != null){ intent intent = new intent(intent.action_send); // 启动分享发送的属性 file file = new file(imagepath); intent.putextra(intent.extra_stream, uri.fromfile(file));// 分享的内容 intent.settype("image/*");// 分享发送的数据类型 intent chooser = intent.createchooser(intent, "share screen shot"); if(intent.resolveactivity(getpackagemanager()) != null){ startactivity(chooser); } } else { toast.maketext(addressselecteractivity.this, "先截屏,再分享", toast.length_short).show(); } } });
截取工具:
//截取屏幕的方法 private void screenshot() { // 获取屏幕 view dview = getwindow().getdecorview(); dview.setdrawingcacheenabled(true); dview.builddrawingcache(); bitmap bmp = dview.getdrawingcache(); if (bmp != null) { try { // 获取内置sd卡路径 string sdcardpath = environment.getexternalstoragedirectory().getpath(); // 图片文件路径 imagepath = sdcardpath + file.separator + "screenshot.png"; file file = new file(imagepath); fileoutputstream os = new fileoutputstream(file); bmp.compress(bitmap.compressformat.png, 100, os); os.flush(); os.close(); } catch (exception e) { } } }
总结
以上所述是小编给大家介绍的android实现截图和分享功能的代码,希望对大家有所帮助
如对本文有疑问, 点击进行留言回复!!
荐 【Android 电量优化】电量优化 ( 网络状态获取 | 主动获取 WIFI 状态 | 被动获取 WIFI 状态 )
Android xhdpi图片转换,能任意转换Android Drawable下“ldpi“, “mdpi“, “hdpi“, “xhdpi“,“xxhdpi“, “xxxhdpi“ 各个类型的图片
unity 之 Bugly(AndroidStudio) 的使用
荐 Android开发7年,疫情期间八家大厂高工的面试经历和真题整理
Android 自定义可编辑可任意全屏拖动的EditText
【android 布局】px和dp的概念和区别,什么情况下用px,dp,sp?
网友评论