日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2024/1/1 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cocos creator 原生安卓接入微信sdk分享图片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

首先還是要先去看微信開發(fā)者文檔
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:+' }

然后構(gòu)建一下? 接入成功后微信的api就能使用了

然后在AppActivity.java文件里面加入兩個變量

?

// APP_ID 替換為你的應(yīng)用從官方網(wǎng)站申請到的合法appIDprivate static final String APP_ID = "你的appid";// IWXAPI 是第三方app和微信通信的openApi接口astatic 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/16447508if (!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 BELOWSDKWrapper.getInstance().init(this);//微信注冊regToWx();} //注冊到微信private void regToWx() {// 通過WXAPIFactory工廠,獲取IWXAPI的實(shí)例api = WXAPIFactory.createWXAPI(this, APP_ID, true);// 將應(yīng)用的appId注冊到微信api.registerApp(APP_ID);//建議動態(tài)監(jiān)聽微信啟動廣播進(jìn)行注冊到微信registerReceiver(new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {// 將該app注冊到微信//api.registerApp(SyncStateContract.Constants._ID);api.registerApp(APP_ID);}}, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));}

然后就可以調(diào)用發(fā)送消息了
?

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;}//發(fā)送圖片給朋友 1:發(fā)送給朋友 2:發(fā)送到朋友圈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;// 設(shè)置縮略圖Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 128, 72, true);msg.thumbData = bmpToByteArray(thumbBmp, true); // Util工具類在微信官方的范例代碼中// 構(gòu)造一個ReqSendMessageToWX.Req req = new SendMessageToWX.Req();req.transaction = buildTransaction("img"); // transaction字段用于唯一標(biāo)識一個請求req.message = msg;//req.scene = WXSceneTimeline;if(type == 1){req.scene = WXSceneSession;}else if(type == 2){req.scene = WXSceneTimeline;}// 調(diào)用api接口發(fā)送數(shù)據(jù)到微信api.sendReq(req);}

前端調(diào)用靜態(tài)方法
?

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

filePath為存在本地的圖片路徑?

微信端提供兩種方法來調(diào)用 一種是本地圖片 一種是bmp圖片數(shù)據(jù)?

這樣就能成功挑起微信并發(fā)送圖片到朋友圈或者發(fā)送給朋友了

總結(jié)

以上是生活随笔為你收集整理的cocos creator 原生安卓接入微信sdk分享图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。