准备:
1.提交应用,得到APP_ID
2.下载SDK,将libammsdk.jar 拷到自己工程的libs中,添加。
代码:
//创建实例
WeChatapi = WXAPIFactory.createWXAPI(ShareMainActivity.this, Constants.WeChat_APP_ID,true);
//检查版本是否支持
int wxSdkVersion = WeChatapi.getWXAppSupportAPI();
if (wxSdkVersion < WeChat_TIMELINE_SUPPORTED_VERSION)
Toast.makeText(ShareMainActivity.this, "版本限制只能发送给好友", Toast.LENGTH_LONG).show();
//注册
WeChatapi.registerApp(Constants.WeChat_APP_ID);
String filePath=Environment.getExternalStorageDirectory()+"/"+picnum+".jpg";
File file = new File(filePath);
if (!file.exists()) {
Toast.makeText(ShareMainActivity.this, "文件不存在" + " path = " + filePath, Toast.LENGTH_LONG).show();
}
else{
WXImageObject imgObj = new WXImageObject();
imgObj.setImagePath(filePath);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
Bitmap bmp = BitmapFactory.decodeFile(filePath);
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
bmp.recycle();
msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("img");
req.message = msg;
req.scene = (wxSdkVersion >= WeChat_TIMELINE_SUPPORTED_VERSION )? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
WeChatapi.sendReq(req);
finish();
备注:
1.其中picnum为要分享的图片的名称。
2.没有涉及授权
3.使用的demo中的图片分享功能,同时分享图片和文字见教程:http://www.cnblogs.com/airshow/archive/2012/11/07/2758016.html