美文网首页
android 分享网页到微信

android 分享网页到微信

作者: hao_developer | 来源:发表于2021-01-05 15:13 被阅读0次

效果图:


image.png

代码

 /**
     * 分享连接到微信
     * @param context  上下文
     * @param shareType  分享类型 1:分享朋友 2:分享朋友圈
     * @param title 分享标题
     * @param describe  分享描述
     * @param url  分享链接
     */
    fun  shareUrlToWx(context: Context,shareType:Int,title:String,describe:String,url:String,id:Int){
        val api = WXAPIFactory.createWXAPI(context, MyParms.APPID, false)
        val webpage = WXWebpageObject()
        webpage.webpageUrl = url
        val msg = WXMediaMessage(webpage)
        msg.title = title
        msg.description = describe
        val thumbBmp = BitmapFactory.decodeResource(context.resources, id)
        msg.thumbData = ImageLoadUtils.bmpToByteArray(thumbBmp,true)
        thumbBmp.recycle()
        val req = SendMessageToWX.Req()
        req.transaction = "webpage" + System.currentTimeMillis()
        req.message = msg
        req.scene = if (shareType == 1)SendMessageToWX.Req.WXSceneSession else SendMessageToWX.Req.WXSceneTimeline
        api.sendReq(req)
    }
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

相关文章

网友评论

      本文标题:android 分享网页到微信

      本文链接:https://www.haomeiwen.com/subject/tyfwoktx.html