提交 65fc30a0 authored 作者: JarvanMo's avatar JarvanMo

fix:crashes on android when sharing image;wrong scene settings

上级 d3a07bfc
......@@ -8,10 +8,9 @@ package com.jarvan.fluwx.constant;
public class WechatPluginKeys {
public static String appId = "";
public static final String SCENE = "scene";
public static final String TIMELINE = "timeline";
public static final String SESSION = "session";
public static final String FAVORITE = "favorite";
public static final String SCENE_TIMELINE = "WeChatScene.TIMELINE";
public static final String SCENE_SESSION = "WeChatScene.SESSION";
public static final String SCENE_FAVORITE = "WeChatScene.FAVORITE";
public static final String TRANSACTION = "transaction";
public static final String TEXT = "text";
......
package com.jarvan.fluwx.handler
import android.util.Log
import com.jarvan.fluwx.constant.CallResult
import com.jarvan.fluwx.constant.WeChatPluginMethods
import com.jarvan.fluwx.constant.WechatPluginKeys
......@@ -15,6 +16,10 @@ import io.flutter.plugin.common.PluginRegistry
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.modelmsg.WXMusicObject
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.launch
/***
......@@ -110,32 +115,56 @@ object WeChatPluginHandler {
}
private suspend fun getImageByteArrayCommon(registrar: PluginRegistry.Registrar?,imagePath:String):ByteArray{
return async(CommonPool){
ShareImageUtil.getImageData(registrar, imagePath)
}.await()
}
private suspend fun getThumbnailByteArrayCommon(registrar: PluginRegistry.Registrar?,thumbnail:String):ByteArray{
return async(CommonPool){
WeChatThumbnailUtil.thumbnailForCommon(thumbnail, registrar)
}.await()
}
private fun shareImage(call: MethodCall, result: MethodChannel.Result) {
val imagePath = call.argument<String>(WechatPluginKeys.IMAGE)
val byteArray = ShareImageUtil.getImageData(registrar, imagePath)
val imgObj = if (byteArray != null) {
WXImageObject(byteArray)
} else {
null
}
if (imgObj == null) {
result.error(CallResult.RESULT_FILE_NOT_EXIST, CallResult.RESULT_FILE_NOT_EXIST, imagePath)
return
}
val msg = WXMediaMessage()
msg.mediaObject = imgObj
msg.thumbData = WeChatThumbnailUtil.thumbnailForCommon(call.argument(WechatPluginKeys.THUMBNAIL), registrar)
launch(UI){
val byteArray :ByteArray? = getImageByteArrayCommon(registrar,imagePath)
val imgObj = if (byteArray != null) {
WXImageObject(byteArray)
} else {
null
}
if (imgObj == null) {
result.error(CallResult.RESULT_FILE_NOT_EXIST, CallResult.RESULT_FILE_NOT_EXIST, imagePath)
return@launch
}
msg.title = call.argument<String>(WechatPluginKeys.TITLE)
msg.description = call.argument<String>(WechatPluginKeys.DESCRIPTION)
Log.e("tag","${byteArray!!.size}")
val msg = WXMediaMessage()
msg.mediaObject = imgObj
var thumbnail:String? = call.argument(WechatPluginKeys.THUMBNAIL)
if (thumbnail.isNullOrBlank()){
thumbnail = imagePath
}
// msg.thumbData = getThumbnailByteArrayCommon(registrar,thumbnail!!)
msg.title = call.argument<String>(WechatPluginKeys.TITLE)
msg.description = call.argument<String>(WechatPluginKeys.DESCRIPTION)
//
val req = SendMessageToWX.Req()
setCommonArguments(call, req, msg)
req.message = msg
result.success(wxApi?.sendReq(req))
val req = SendMessageToWX.Req()
setCommonArguments(call, req, msg)
req.message = msg
result.success(wxApi?.sendReq(req))
}
}
private fun shareMusic(call: MethodCall, result: MethodChannel.Result) {
......@@ -156,6 +185,7 @@ object WeChatPluginHandler {
msg.thumbData = WeChatThumbnailUtil.thumbnailForCommon(thumbnail, registrar)
}
val req = SendMessageToWX.Req()
setCommonArguments(call, req, msg)
req.message = msg
......@@ -240,10 +270,10 @@ object WeChatPluginHandler {
}
private fun getScene(value: String) = when (value.toLowerCase()) {
WechatPluginKeys.TIMELINE -> SendMessageToWX.Req.WXSceneTimeline
WechatPluginKeys.SESSION -> SendMessageToWX.Req.WXSceneSession
WechatPluginKeys.FAVORITE -> SendMessageToWX.Req.WXSceneFavorite
private fun getScene(value: String) = when (value) {
WechatPluginKeys.SCENE_TIMELINE -> SendMessageToWX.Req.WXSceneTimeline
WechatPluginKeys.SCENE_SESSION -> SendMessageToWX.Req.WXSceneSession
WechatPluginKeys.SCENE_FAVORITE -> SendMessageToWX.Req.WXSceneFavorite
else -> SendMessageToWX.Req.WXSceneTimeline
}
......@@ -251,7 +281,6 @@ object WeChatPluginHandler {
msg.messageAction = call.argument<String>(WechatPluginKeys.MESSAGE_ACTION)
msg.messageExt = call.argument<String>(WechatPluginKeys.MESSAGE_EXT)
msg.mediaTagName = call.argument<String>(WechatPluginKeys.MEDIA_TAG_NAME)
req.transaction = call.argument(WechatPluginKeys.TRANSACTION)
req.scene = getScene(call.argument(WechatPluginKeys.SCENE))
}
......
......@@ -59,4 +59,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:5.1.4'
}
......@@ -35,5 +35,6 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".wxapi.WXEntryActivity" />
</application>
</manifest>
package com.jarvan.fluwxexample.wxapi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import com.jarvan.fluwx.handler.WeChatPluginHandler;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
private Button gotoBtn, regBtn, launchBtn, checkBtn, scanBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
@Override
public void onReq(BaseReq req) {
}
@Override
public void onResp(BaseResp resp) {
WeChatPluginHandler.INSTANCE.onResp(resp);
}
}
\ No newline at end of file
......@@ -56,8 +56,12 @@ class _MyAppState extends State<MyApp> {
child: new FlatButton(
onPressed: () {
var fluwx = Fluwx();
fluwx.share(WeChatShareTextModel(
text: "share text from flutter", transaction: "hehe"));
fluwx.share(WeChatShareImageModel(
image: "https://www.baidu.com/img/bd_logo1.png",
transaction: "hehe",
title: "from dart",
scene: WeChatScene.SESSION
));
},
child: new Text("share text to wechat")),
),
......
......@@ -116,7 +116,6 @@ class WeChatShareMiniProgramModel extends WeChatShareModel {
class WeChatShareImageModel extends WeChatShareModel {
final String transaction;
final WeChatScene scene;
final String image;
final String thumbnail;
final String title;
......@@ -133,7 +132,6 @@ class WeChatShareImageModel extends WeChatShareModel {
String mediaTagName,
this.title})
: this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE,
this.thumbnail = thumbnail ?? "",
assert(image != null),
super(
......@@ -160,7 +158,6 @@ class WeChatShareImageModel extends WeChatShareModel {
class WeChatShareMusicModel extends WeChatShareModel {
final String transaction;
final WeChatScene scene;
final String musicUrl;
final String musicLowBandUrl;
final String thumbnail;
......@@ -179,7 +176,6 @@ class WeChatShareMusicModel extends WeChatShareModel {
String messageAction,
String mediaTagName,
}) : this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE,
this.thumbnail = thumbnail ?? "",
assert(musicUrl != null || musicLowBandUrl != null),
assert(thumbnail != null),
......@@ -208,7 +204,6 @@ class WeChatShareMusicModel extends WeChatShareModel {
class WeChatShareVideoModel extends WeChatShareModel {
final String transaction;
final WeChatScene scene;
final String videoUrl;
final String videoLowBandUrl;
final String thumbnail;
......@@ -231,7 +226,6 @@ class WeChatShareVideoModel extends WeChatShareModel {
this.messageAction,
this.mediaTagName,
}) : this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE,
this.thumbnail = thumbnail ?? "",
assert(videoUrl != null || videoLowBandUrl != null),
assert(thumbnail != null),
......@@ -260,7 +254,6 @@ class WeChatShareVideoModel extends WeChatShareModel {
class WeChatShareWebPageModel extends WeChatShareModel {
final String transaction;
final WeChatScene scene;
final String webPage;
final String thumbnail;
final String title;
......@@ -277,7 +270,6 @@ class WeChatShareWebPageModel extends WeChatShareModel {
String messageAction,
String mediaTagName,
}) : this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE,
this.thumbnail = thumbnail ?? "",
assert(webPage != null),
assert(thumbnail != null),
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论