cocos creator 原生安卓接入微信sdk分享图片

首先还是要先去看微信开发者文档
https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Share_and_Favorites/Android.html


下载微信的范例项目可以更加快速接入,不然看文档会一脸懵逼


首先安卓端要引入微信sdk,在项目的build.gradle的 dependencies里面引入
 

api 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
dependencies {
    implementation fileTree(dir: '../libs', include: ['*.jar','*.aar'])
    implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
    implementation fileTree(dir: "C:/CocosDashboard_1.0.12/resources/.editors/Creator/2.4.4/resources/cocos2d-x/cocos/platform/android/java/libs", include: ['*.jar'])
    implementation project(':libcocos2dx')
    //implementation(name: 'open_ad_sdk', ext: 'aar')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    //implementation 'com.android.support:support-v4:24.2.0'
    api 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}

然后构建一下  接入成功后微信的api就能使用了

然后在AppActivity.java文件里面加入两个变量

 

  // APP_ID 替换为你的应用从官方网站申请到的合法appID
    private static final String APP_ID = "你的appid";

    // IWXAPI 是第三方app和微信通信的openApi接口a
    static public IWXAPI api;

在onCreate的方法里面先注册到微信端
 

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Workaround in
        // https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
        if (!isTaskRoot()) {
            // Android launched another instance of the root activity into an existing task
            // so just quietly finish and go away, dropping the user back into the activity
            // at the top of the stack (ie: the last state of this task)
            // Don't need to finish it again since it's finished in super.onCreate .
            return;
        }
        // DO OTHER INITIALIZATION BELOW
        SDKWrapper.getInstance().init(this);

 

         //微信注册
        regToWx();

    }
 //注册到微信
    private void regToWx() {
        // 通过WXAPIFactory工厂,获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, APP_ID, true);

        // 将应用的appId注册到微信
        api.registerApp(APP_ID);

        //建议动态监听微信启动广播进行注册到微信
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                // 将该app注册到微信
                //api.registerApp(SyncStateContract.Constants._ID);
                api.registerApp(APP_ID);
            }
        }, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));
    }

然后就可以调用发送消息了
 

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

    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;
    }

    //发送图片给朋友  1:发送给朋友 2:发送到朋友圈
    public static void sendPengyouquan1(String path,int type){

        // 初始化WXImageObject对象
        Bitmap bmp = BitmapFactory.decodeFile(path);
        WXImageObject imgObj = new WXImageObject(bmp);

        // 初始化WXMediaMessage对象
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

        // 设置缩略图
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 128, 72, true);
        msg.thumbData = bmpToByteArray(thumbBmp, true); // Util工具类在微信官方的范例代码中

        // 构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img"); // transaction字段用于唯一标识一个请求
        req.message = msg;

        //req.scene = WXSceneTimeline;
        if(type == 1){
            req.scene = WXSceneSession;
        }
        else if(type == 2){
            req.scene = WXSceneTimeline;
        }

        // 调用api接口发送数据到微信
        api.sendReq(req);
    }

前端调用静态方法
 

jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "sendPengyouquan1", "(Ljava/lang/String;I)V", filePath, type);

filePath为存在本地的图片路径 

微信端提供两种方法来调用 一种是本地图片 一种是bmp图片数据 

这样就能成功挑起微信并发送图片到朋友圈或者发送给朋友了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos Creator是一款流行的游戏开发引擎,它提供了方便的工具和功能,可以帮助开发者在多个平台上创建游戏,包括微信小游戏平台。 接入微信小游戏的步骤如下: 1. 准备工作: - 确保你已经安装并配置好了Cocos Creator开发环境。 - 在微信公众平台上注册一个小程序账号,并获取到小程序的AppID。 2. 创建项目: - 打开Cocos Creator,选择新建项目,并选择微信小游戏项目模板。 - 配置项目信息,包括项目名称、路径等。 3. 项目设置: - 在项目设置中,选择微信小游戏平台,并填写小程序的AppID。 - 根据需要配置其他相关设置,如屏幕适配、引擎版本等。 4. 开发游戏: - 使用Cocos Creator提供的编辑器和工具进行游戏开发。 - 可以使用JavaScriptTypeScript编写代码,创建场景、精灵、动画等。 5. 调试与预览: - 在Cocos Creator中,可以选择微信小游戏平台进行调试和预览。 - 在微信开发者工具中,导入Cocos Creator生成的小游戏项目,并进行调试、预览和测试。 6. 发布与上线: - 在Cocos Creator中,选择发布小游戏,并按照提示进行配置和打包。 - 将生成的小游戏包上传至微信开放平台,并进行审核和发布。 以上是接入微信小游戏的基本步骤,具体的细节和操作步骤可以参考Cocos Creator的官方文档和微信小游戏的开发文档,以获得更详细的指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值