提交 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; ...@@ -8,10 +8,9 @@ package com.jarvan.fluwx.constant;
public class WechatPluginKeys { public class WechatPluginKeys {
public static String appId = ""; public static String appId = "";
public static final String SCENE = "scene"; public static final String SCENE = "scene";
public static final String TIMELINE = "timeline"; public static final String SCENE_TIMELINE = "WeChatScene.TIMELINE";
public static final String SESSION = "session"; public static final String SCENE_SESSION = "WeChatScene.SESSION";
public static final String FAVORITE = "favorite"; public static final String SCENE_FAVORITE = "WeChatScene.FAVORITE";
public static final String TRANSACTION = "transaction"; public static final String TRANSACTION = "transaction";
public static final String TEXT = "text"; public static final String TEXT = "text";
......
package com.jarvan.fluwx.handler package com.jarvan.fluwx.handler
import android.util.Log
import com.jarvan.fluwx.constant.CallResult import com.jarvan.fluwx.constant.CallResult
import com.jarvan.fluwx.constant.WeChatPluginMethods import com.jarvan.fluwx.constant.WeChatPluginMethods
import com.jarvan.fluwx.constant.WechatPluginKeys import com.jarvan.fluwx.constant.WechatPluginKeys
...@@ -15,6 +16,10 @@ import io.flutter.plugin.common.PluginRegistry ...@@ -15,6 +16,10 @@ import io.flutter.plugin.common.PluginRegistry
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.modelmsg.WXMusicObject 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,9 +115,24 @@ object WeChatPluginHandler { ...@@ -110,9 +115,24 @@ 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) { private fun shareImage(call: MethodCall, result: MethodChannel.Result) {
val imagePath = call.argument<String>(WechatPluginKeys.IMAGE) val imagePath = call.argument<String>(WechatPluginKeys.IMAGE)
val byteArray = ShareImageUtil.getImageData(registrar, imagePath)
launch(UI){
val byteArray :ByteArray? = getImageByteArrayCommon(registrar,imagePath)
val imgObj = if (byteArray != null) { val imgObj = if (byteArray != null) {
WXImageObject(byteArray) WXImageObject(byteArray)
} else { } else {
...@@ -121,12 +141,17 @@ object WeChatPluginHandler { ...@@ -121,12 +141,17 @@ object WeChatPluginHandler {
if (imgObj == null) { if (imgObj == null) {
result.error(CallResult.RESULT_FILE_NOT_EXIST, CallResult.RESULT_FILE_NOT_EXIST, imagePath) result.error(CallResult.RESULT_FILE_NOT_EXIST, CallResult.RESULT_FILE_NOT_EXIST, imagePath)
return return@launch
} }
Log.e("tag","${byteArray!!.size}")
val msg = WXMediaMessage() val msg = WXMediaMessage()
msg.mediaObject = imgObj msg.mediaObject = imgObj
msg.thumbData = WeChatThumbnailUtil.thumbnailForCommon(call.argument(WechatPluginKeys.THUMBNAIL), registrar) 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.title = call.argument<String>(WechatPluginKeys.TITLE)
msg.description = call.argument<String>(WechatPluginKeys.DESCRIPTION) msg.description = call.argument<String>(WechatPluginKeys.DESCRIPTION)
...@@ -138,6 +163,10 @@ object WeChatPluginHandler { ...@@ -138,6 +163,10 @@ object WeChatPluginHandler {
result.success(wxApi?.sendReq(req)) result.success(wxApi?.sendReq(req))
} }
}
private fun shareMusic(call: MethodCall, result: MethodChannel.Result) { private fun shareMusic(call: MethodCall, result: MethodChannel.Result) {
val music = WXMusicObject() val music = WXMusicObject()
val musicUrl: String? = call.argument("musicUrl") val musicUrl: String? = call.argument("musicUrl")
...@@ -156,6 +185,7 @@ object WeChatPluginHandler { ...@@ -156,6 +185,7 @@ object WeChatPluginHandler {
msg.thumbData = WeChatThumbnailUtil.thumbnailForCommon(thumbnail, registrar) msg.thumbData = WeChatThumbnailUtil.thumbnailForCommon(thumbnail, registrar)
} }
val req = SendMessageToWX.Req() val req = SendMessageToWX.Req()
setCommonArguments(call, req, msg) setCommonArguments(call, req, msg)
req.message = msg req.message = msg
...@@ -240,10 +270,10 @@ object WeChatPluginHandler { ...@@ -240,10 +270,10 @@ object WeChatPluginHandler {
} }
private fun getScene(value: String) = when (value.toLowerCase()) { private fun getScene(value: String) = when (value) {
WechatPluginKeys.TIMELINE -> SendMessageToWX.Req.WXSceneTimeline WechatPluginKeys.SCENE_TIMELINE -> SendMessageToWX.Req.WXSceneTimeline
WechatPluginKeys.SESSION -> SendMessageToWX.Req.WXSceneSession WechatPluginKeys.SCENE_SESSION -> SendMessageToWX.Req.WXSceneSession
WechatPluginKeys.FAVORITE -> SendMessageToWX.Req.WXSceneFavorite WechatPluginKeys.SCENE_FAVORITE -> SendMessageToWX.Req.WXSceneFavorite
else -> SendMessageToWX.Req.WXSceneTimeline else -> SendMessageToWX.Req.WXSceneTimeline
} }
...@@ -251,7 +281,6 @@ object WeChatPluginHandler { ...@@ -251,7 +281,6 @@ object WeChatPluginHandler {
msg.messageAction = call.argument<String>(WechatPluginKeys.MESSAGE_ACTION) msg.messageAction = call.argument<String>(WechatPluginKeys.MESSAGE_ACTION)
msg.messageExt = call.argument<String>(WechatPluginKeys.MESSAGE_EXT) msg.messageExt = call.argument<String>(WechatPluginKeys.MESSAGE_EXT)
msg.mediaTagName = call.argument<String>(WechatPluginKeys.MEDIA_TAG_NAME) msg.mediaTagName = call.argument<String>(WechatPluginKeys.MEDIA_TAG_NAME)
req.transaction = call.argument(WechatPluginKeys.TRANSACTION) req.transaction = call.argument(WechatPluginKeys.TRANSACTION)
req.scene = getScene(call.argument(WechatPluginKeys.SCENE)) req.scene = getScene(call.argument(WechatPluginKeys.SCENE))
} }
......
...@@ -59,4 +59,5 @@ dependencies { ...@@ -59,4 +59,5 @@ dependencies {
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.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 @@ ...@@ -35,5 +35,6 @@
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".wxapi.WXEntryActivity" />
</application> </application>
</manifest> </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> { ...@@ -56,8 +56,12 @@ class _MyAppState extends State<MyApp> {
child: new FlatButton( child: new FlatButton(
onPressed: () { onPressed: () {
var fluwx = Fluwx(); var fluwx = Fluwx();
fluwx.share(WeChatShareTextModel( fluwx.share(WeChatShareImageModel(
text: "share text from flutter", transaction: "hehe")); image: "https://www.baidu.com/img/bd_logo1.png",
transaction: "hehe",
title: "from dart",
scene: WeChatScene.SESSION
));
}, },
child: new Text("share text to wechat")), child: new Text("share text to wechat")),
), ),
......
...@@ -116,7 +116,6 @@ class WeChatShareMiniProgramModel extends WeChatShareModel { ...@@ -116,7 +116,6 @@ class WeChatShareMiniProgramModel extends WeChatShareModel {
class WeChatShareImageModel extends WeChatShareModel { class WeChatShareImageModel extends WeChatShareModel {
final String transaction; final String transaction;
final WeChatScene scene;
final String image; final String image;
final String thumbnail; final String thumbnail;
final String title; final String title;
...@@ -133,7 +132,6 @@ class WeChatShareImageModel extends WeChatShareModel { ...@@ -133,7 +132,6 @@ class WeChatShareImageModel extends WeChatShareModel {
String mediaTagName, String mediaTagName,
this.title}) this.title})
: this.transaction = transaction ?? "text", : this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE,
this.thumbnail = thumbnail ?? "", this.thumbnail = thumbnail ?? "",
assert(image != null), assert(image != null),
super( super(
...@@ -160,7 +158,6 @@ class WeChatShareImageModel extends WeChatShareModel { ...@@ -160,7 +158,6 @@ class WeChatShareImageModel extends WeChatShareModel {
class WeChatShareMusicModel extends WeChatShareModel { class WeChatShareMusicModel extends WeChatShareModel {
final String transaction; final String transaction;
final WeChatScene scene;
final String musicUrl; final String musicUrl;
final String musicLowBandUrl; final String musicLowBandUrl;
final String thumbnail; final String thumbnail;
...@@ -179,7 +176,6 @@ class WeChatShareMusicModel extends WeChatShareModel { ...@@ -179,7 +176,6 @@ class WeChatShareMusicModel extends WeChatShareModel {
String messageAction, String messageAction,
String mediaTagName, String mediaTagName,
}) : this.transaction = transaction ?? "text", }) : this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE,
this.thumbnail = thumbnail ?? "", this.thumbnail = thumbnail ?? "",
assert(musicUrl != null || musicLowBandUrl != null), assert(musicUrl != null || musicLowBandUrl != null),
assert(thumbnail != null), assert(thumbnail != null),
...@@ -208,7 +204,6 @@ class WeChatShareMusicModel extends WeChatShareModel { ...@@ -208,7 +204,6 @@ class WeChatShareMusicModel extends WeChatShareModel {
class WeChatShareVideoModel extends WeChatShareModel { class WeChatShareVideoModel extends WeChatShareModel {
final String transaction; final String transaction;
final WeChatScene scene;
final String videoUrl; final String videoUrl;
final String videoLowBandUrl; final String videoLowBandUrl;
final String thumbnail; final String thumbnail;
...@@ -231,7 +226,6 @@ class WeChatShareVideoModel extends WeChatShareModel { ...@@ -231,7 +226,6 @@ class WeChatShareVideoModel extends WeChatShareModel {
this.messageAction, this.messageAction,
this.mediaTagName, this.mediaTagName,
}) : this.transaction = transaction ?? "text", }) : this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE,
this.thumbnail = thumbnail ?? "", this.thumbnail = thumbnail ?? "",
assert(videoUrl != null || videoLowBandUrl != null), assert(videoUrl != null || videoLowBandUrl != null),
assert(thumbnail != null), assert(thumbnail != null),
...@@ -260,7 +254,6 @@ class WeChatShareVideoModel extends WeChatShareModel { ...@@ -260,7 +254,6 @@ class WeChatShareVideoModel extends WeChatShareModel {
class WeChatShareWebPageModel extends WeChatShareModel { class WeChatShareWebPageModel extends WeChatShareModel {
final String transaction; final String transaction;
final WeChatScene scene;
final String webPage; final String webPage;
final String thumbnail; final String thumbnail;
final String title; final String title;
...@@ -277,7 +270,6 @@ class WeChatShareWebPageModel extends WeChatShareModel { ...@@ -277,7 +270,6 @@ class WeChatShareWebPageModel extends WeChatShareModel {
String messageAction, String messageAction,
String mediaTagName, String mediaTagName,
}) : this.transaction = transaction ?? "text", }) : this.transaction = transaction ?? "text",
this.scene = scene ?? WeChatScene.TIMELINE,
this.thumbnail = thumbnail ?? "", this.thumbnail = thumbnail ?? "",
assert(webPage != null), assert(webPage != null),
assert(thumbnail != null), assert(thumbnail != null),
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论