一键发圈,一键转发到微信朋友圈或者微信好友

转载地址:http://blog.csdn.net/zflove168/article/details/79216773


package com.derivative.client.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.derivative.client.R;

import java.io.File;
import java.util.ArrayList;

/**
 * 拉起微信,朋友圈功能类,支持单张图片,多张图片,文字
 */
public class ShareUtils {

    PopupWindow popupWindow;
    Context context;
    private String path;//单张图片路径
    private String content;
    private Button btn;
    private Uri[] uris;//多张图片路径uri数组
    public ShareUtils(Context context, String content){
        this.context=context;
      //  this.path=path;
        this.content=content;

      //  this.btn=btn;
        
        showpop();
    }

    public void setUri(Uri[] uris){
        this.uris = uris;
    }

    public void setPath(String path){
        this.path = path;
    }

    private void showpop() {
        View view= LayoutInflater.from(context).inflate(
                R.layout.share_view, null);
        ImageView img_weixin= (ImageView) view.findViewById(R.id.share_weixin);
        ImageView img_pyq= (ImageView) view.findViewById(R.id.share_pengyouquan);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable()); // 点击返回按钮popwindow消失

        img_weixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isWeixinAvilible(context)) {// 判断是否安装微信客户端
                   // shareweixin(path);
                    shareWXSomeImg(context,uris);
                    // login(SHARE_MEDIA.WEIXIN);
                } else {
                    ActivityUtil.showToast(context, "请安装微信客户端");
                }

                popupWindow.dismiss();
            }
        });
        img_pyq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (StringUtils.isWeixinAvilible(context)) {// 判断是否安装微信客户端
                 //   shareweipyq(path,content);//拉起微信朋友圈带一张图片
                    shareweipyqSomeImg(context,uris);//拉起微信朋友圈带多张图片
                    // login(SHARE_MEDIA.WEIXIN);
                } else {
                    ActivityUtil.showToast(context, "请安装微信客户端");
                }
                popupWindow.dismiss();
            }
        });

        popupWindow.showAtLocation( LayoutInflater.from(context).inflate(
                R.layout.activity_posterxq, null).findViewById(R.id.img_share), Gravity.BOTTOM, 0, 0);// 先设置popwindow的所有参数,最后再show

    }

    /**
     * 拉起微信好友发送单张图片
     * */
    private void shareweixin(String path){
        Uri uriToImage = Uri.fromFile(new File(path));
        Intent shareIntent = new Intent();
        //发送图片到朋友圈
        //ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
        //发送图片给好友。
        ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
        shareIntent.setComponent(comp);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
        shareIntent.setType("image/jpeg");
        context.startActivity(Intent.createChooser(shareIntent, "分享图片"));
    }
    /**
     * 拉起微信朋友圈发送单张图片
     * */
    private void shareweipyq(String path,String content){
        Uri uriToImage = Uri.fromFile(new File(path));
        Intent shareIntent = new Intent();
        //发送图片到朋友圈
        ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
        //发送图片给好友。
//        ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
        shareIntent.setComponent(comp);
        shareIntent.putExtra("Kdescription", content);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
        shareIntent.setType("image/jpeg");
        context.startActivity(Intent.createChooser(shareIntent, "分享图片"));
    }

    /**
     * 拉起微信朋友圈发送多张图片
     * */
    private void shareweipyqSomeImg(Context context,Uri[] uri){
        Intent shareIntent = new Intent();
        //1调用系统分析
        shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        //2添加图片数组
        ArrayList<Uri> imageUris = new ArrayList<>();
        for (int i = 0; i < uri.length; i++) {
            imageUris.add(uri[i]);
        }

        shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);
        shareIntent.setType("image/*");

        //3指定选择微信
        ComponentName componentName = new ComponentName("com.tencent.mm","com.tencent.mm.ui.tools.ShareToTimeLineUI");
        shareIntent.setComponent(componentName);

        //4开始分享
        context.startActivity(Intent.createChooser(shareIntent,"分享图片"));
    }

    /**
     * 拉起微信发送多张图片给好友
     * */
    private void shareWXSomeImg(Context context,Uri[] uri){
        Intent shareIntent = new Intent();
        //1调用系统分析
        shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        //2添加图片数组
        ArrayList<Uri> imageUris = new ArrayList<>();
        for (int i = 0; i < uri.length; i++) {
            imageUris.add(uri[i]);
        }

        shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);
        shareIntent.setType("image/*");

        //3指定选择微信
        ComponentName componentName = new ComponentName("com.tencent.mm","com.tencent.mm.ui.tools.ShareImgUI");
        shareIntent.setComponent(componentName);

        //4开始分享
        context.startActivity(Intent.createChooser(shareIntent,"分享图片"));
    }
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值