注意图片大小
// 分享text
// 分享text
public void wxShareText(JSONObject jsonObj){
try {
Log.d("wxShareText","wxShareText");
String text = jsonObj.getString("text");
int platformID = jsonObj.getInt("shareScene");
WXTextObject textObject = new WXTextObject();
textObject.text = text;
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObject;
msg.description = text;
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = "text";
req.message = msg;
req.scene = platformID;
wxApi.sendReq(req);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
// 分享图片(本地图片)
public void wxShareImage(JSONObject jsonObj){
try {
Log.d("wxShareImage","wxShareImage");
String imgName = jsonObj.getString("imgName");
int platformID = jsonObj.getInt("shareScene");
Bitmap bmp = BitmapFactory.decodeFile(imgName);
WXImageObject imgObj = new WXImageObject(bmp);
//
imgObj.setImagePath(imgName);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
msg.setThumbImage(thumbBmp);
//
msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
bmp.recycle();
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = msg;
req.scene = platformID;
wxApi.sendReq(req);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
private Bitmap decodeUriAsBitmapFromNet(String url) {
URL fileUrl = null;
Bitmap bitmap = null;
try {
fileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) fileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
// 分享网页
public void wxShareWeb(JSONObject jsonObj){
try {
Log.d("wxShareWeb","wxShareWeb");
String imgName = jsonObj.getString("imgName");
int platformID = jsonObj.getInt("shareScene");
String title = jsonObj.getString("title");
String text = jsonObj.getString("text");
String targeturl = jsonObj.getString("targeturl");
WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl = targeturl;
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title = title;
msg.description = text;
if ( imgName.indexOf("http:") != -1 ){
try{
Bitmap thumb = BitmapFactory.decodeStream(new URL(imgName).openStream());
//
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//
thumb.compress(Bitmap.CompressFormat.PNG, 100, baos);
//
//
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
//
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//
// 开始读入图片,此时把options.inJustDecodeBounds 设回true了
//
newOpts.inJustDecodeBounds = true;
//
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
//
newOpts.inJustDecodeBounds = false;
//
//
newOpts.inSampleSize = 2; // 设置缩放比例
//
//
isBm = new ByteArrayInputStream(baos.toByteArray());
//
bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
Bitmap thumbBmp = Bitmap.createScaledBitmap(thumb,150,150,true);
thumb.recycle();
msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
}catch(IOException e) {
e.printStackTrace();
}
}else{
Bitmap thumb = BitmapFactory.decodeFile(imgName);
Bitmap thumbBmp = Bitmap.createScaledBitmap(thumb, 150, 150, true);
thumb.recycle();
msg.setThumbImage(thumbBmp);
}
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = "webpage";
req.message = msg;
req.scene = platformID;
wxApi.sendReq(req);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}