我是用这段代码分享的:
public class WxUtils {
private static final String APP_ID = "xxx";
private IWXAPI api;
public void regToWx(Context context) {
api = WXAPIFactory.createWXAPI(context, APP_ID, true);
api.registerApp(APP_ID);
WeakReference<WxUtils> self = new WeakReference<>(this);
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (self.get() != null) {
self.get().api.registerApp(APP_ID);
}
}
}, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));
}
public boolean shareToMoments(String url, String text, String description, Bitmap thumbnail, boolean transparent) {
if (api.getWXAppSupportAPI() >= Build.TIMELINE_SUPPORTED_SDK_INT) {
share(url, text, description, saveBitmap(thumbnail, transparent), SendMessageToWX.Req.WXSceneTimeline);
return true;
} else {
return false;
}
}
public void shareToChat(String url, String text, String description, Bitmap thumbnail, boolean transparent) {
share(url, text, description, saveBitmap(thumbnail, transparent), SendMessageToWX.Req.WXSceneSession);
}
private void share(String url, String text, String description, byte[] thumbnailBytes, int scene) {
WXWebpageObject webPage = new WXWebpageObject();
webPage.webpageUrl = url;
WXMediaMessage msg = new WXMediaMessage(webPage);
msg.title = text;
msg.description = description;
msg.thumbData = thumbnailBytes;
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = IoUtils.createUuidStr();
req.message = msg;
req.scene = scene;
api.sendReq(req);
}
private byte[] saveBitmap(Bitmap bitmap, boolean transparent) {
Bitmap newBitmap;
if (transparent) {
newBitmap = BitmapUtils.scaleBitmap(bitmap, 64, 64);
} else {
newBitmap = BitmapUtils.scaleBitmap(bitmap, 256, 256);
}
ByteArrayOutputStream os = new ByteArrayOutputStream(1024 * 32);
Bitmap.CompressFormat format = transparent ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG;
newBitmap.compress(format, 80, os);
return os.toByteArray();
}
}
【 在 hxf (work hard !!!) 的大作中提到: 】
: 【 以下文字转载自 Programming 讨论区 】
: 发信人: hxf (work hard !!!), 信区: Programming
: 标 题: 有懂安卓的大佬吗
: ...................
--
FROM 112.47.122.*