Android实现截图分享qq 微信功能

发布时间:2020-10-21 08:43:47 作者:mrr
来源:脚本之家 阅读:184

在上篇文章给大家介绍了Android实现截图和分享功能的代码。感兴趣可以点击阅读,今天通过本文给大家介绍Android实现截图分享qq 微信功能。一起看看吧。

前言

现在很多应用都有截图分享的功能,今天就来讲讲截图分享吧

今天涉及到以下内容:

ok,下面就来具体讲讲

一.权限,注意权限

先在自己的mainfast中添加以下权限:

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

然后是要申请Android7.0以上的权限,之前讲过了,这里就不再废话了。

二.截图分享类

代码如下:

package com.dialogfragmentdemo.util;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import java.io.File;
import java.io.FileOutputStream;
/**
 * Title:截屏分享
 * Description:
 * 需要用户读写权限
 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 * <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 *
 * Created by pei
 * Date: 2017/12/6
 */
public class ShotShareUtil {
 /**截屏分享,供外部调用**/
 public static void shotShare(Context context){
 //截屏
 String path=screenShot(context);
 //分享
 if(StringUtil.isNotEmpty(path)){
  ShareImage(context,path);
 }
 }
 /**获取截屏**/
 private static String screenShot(Context context){
 String imagePath=null;
 Bitmap bitmap= ScreenUtil.snapShotWithoutStatusBar(context);
 if(bitmap!=null){
  try {
  // 图片文件路径
  imagePath = SDCardUtil.getDiskCachePath()+"share.png";
  LogUtil.e(ShotShareUtil.class, "====imagePath====" + imagePath);
  File file = new File(imagePath);
  FileOutputStream os = new FileOutputStream(file);
  bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
  os.flush();
  os.close();
  return imagePath;
  } catch (Exception e) {
  LogUtil.e(ShotShareUtil.class, "====screenshot:error====" + e.getMessage());
  }
 }
 return null;
 }
 /**分享**/
 private static void ShareImage(Context context,String imagePath){
 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(context.getPackageManager()) != null){
  context.startActivity(chooser);
  }
 } else {
  ToastUtil.shortShow("先截屏,再分享");
 }
 }
}

三.在mainactivity中调用

以下是示例代码:

@Override
 public void onClick(View v) {
 super.onClick(v);
 switch (v.getId()) {
  case R.id.button:
  LogUtil.e(MainActivity.class,"====我点击了====");
  //截屏分享
  ShotShareUtil.shotShare(mContext);
  break;
  default:
  break;
 }
 }

四.效果图

Android实现截图分享qq 微信功能

上面是分享的时候,手机上没装qq和微信的情况,下面展示有qq,微信的情况

Android实现截图分享qq 微信功能

总结

以上所述是小编给大家介绍的Android实现截图分享qq 微信功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

推荐阅读:
  1. iOS如何实现微信分享功能
  2. 怎么在android中实现分享纯图片到QQ空间

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android 截图分享

上一篇:addeventlistener监听scroll跟touch(实例讲解)

下一篇:python将数组n等分的实例

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》