当前位置: 移动技术网 > 移动技术>移动开发>Android > Android实现截图和分享功能的代码

Android实现截图和分享功能的代码

2019年07月24日  | 移动技术网移动技术  | 我要评论

先给大家展示下效果图吧

直接上代码:

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实现截图和分享功能的代码,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网