分享小程序

在grdle里

implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'

在WXEntryActivity类

@Override
public void onResp(BaseResp baseResp) {
    int errCode = baseResp.errCode;
    if (baseResp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
        WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) baseResp;
        String extraData =launchMiniProResp.extMsg; //对应小程序组件 

接下来再需要的地方写如下代码:

private void shareMiniProgram() {
    String appId = ConstantValues.APP_ID; // 填应用AppId
    IWXAPI api = WXAPIFactory.createWXAPI(mContext, appId);

    WXMiniProgramObject miniProgramObj = new WXMiniProgramObject();
    miniProgramObj.webpageUrl = "http://www.qq.com"; // 兼容低版本的网页链接
    miniProgramObj.miniprogramType = WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE;// 正式版:0,测试版:1,体验版:2
    miniProgramObj.userName = "gh_890e142ba85";     // 小程序原始id
    miniProgramObj.path = “pages/xxx/xxx”;            //小程序页面路径;对于小游戏,可以只传入 query 部分,来实现传参效果,如:传入 "?foo=bar"
    WXMediaMessage msg = new WXMediaMessage(miniProgramObj);
    msg.title = “title”;           // 小程序消息title
    msg.description = "描述";  // 小程序消息desc
    
    Bitmap bitmap = ImageUtil.getBitMBitmap(mGoodsDetial.getCover());
    byte[] bytes = ImageUtil.compressByQuality(bitmap, 120, true);
    msg.thumbData = bytes;
    
    SendMessageToWX.Req req = new SendMessageToWX.Req();
    req.transaction = buildTransaction("miniProgram");
    req.message = msg;
    req.scene = SendMessageToWX.Req.WXSceneSession;  // 目前只支持会话
    api.sendReq(req);
}

private static String buildTransaction(final String type) {
    return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
}

详细说一下这个加载小程序封面图的问题,也就是如下代码:
Bitmap bitmap = ImageUtil.getBitMBitmap(mGoodsDetial.getCover());
byte[] bytes = ImageUtil.compressByQuality(bitmap, 120, true);
msg.thumbData = bytes;

第一种情况加载本地图片的时候:可将以上代码换成下边即可
Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(),R.drawable.fightk);
Bitmap sendBitmap = Bitmap.createScaledBitmap(bitmap,200,200,true);
bitmap.recycle();
mediaMessage.thumbData = Util.bmpToByteArray(sendBitmap,true);

第二种加载图片url,也就是以上所写的那样,因为图片超过128k无法分享,所以要将其压缩
另外涉及到的两个方法:

/**
 * 图片url转bitmap
 */
public static Bitmap getBitMBitmap(String urlpath) {
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    Bitmap map = null;
    try {
        URL url = new URL(urlpath);
        URLConnection conn = url.openConnection();
        conn.connect();
        InputStream in;
        in = conn.getInputStream();
        map = BitmapFactory.decodeStream(in);
        // TODO Auto-generated catch block
    } catch (IOException e) {
        e.printStackTrace();
    }
    return map;
}


/**
 * 按质量压缩图片
 *
 * @param src         bitmap图片
 * @param maxByteSize 最大字节数
 * @param recycle
 * @return
 */
public static byte[] compressByQuality(final Bitmap src, final long maxByteSize, final boolean recycle) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    src.compress(CompressFormat.JPEG, 100, baos);
    byte[] bytes;
    if (baos.size() <= maxByteSize) {
        bytes = baos.toByteArray();
    } else {
        baos.reset();
        src.compress(CompressFormat.JPEG, 0, baos);
        if (baos.size() >= maxByteSize) {
            bytes = baos.toByteArray();
        } else {
            // find the best quality using binary search
            int st = 0;
            int end = 100;
            int mid = 0;
            while (st < end) {
                mid = (st + end) / 2;
                baos.reset();
                src.compress(CompressFormat.JPEG, mid, baos);
                int len = baos.size();
                if (len == maxByteSize) {
                    break;
                } else if (len > maxByteSize) {
                    end = mid - 1;
                } else {
                    st = mid + 1;
                }
            }
            if (end == mid - 1) {
                baos.reset();
                src.compress(CompressFormat.JPEG, st, baos);
            }
            bytes = baos.toByteArray();
        }
    }
    if (recycle && !src.isRecycled()) src.recycle();
    return bytes;
}

你可能感兴趣的:(分享小程序)