123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package com.example.myapplication.utils;
- import android.content.Context;
- import android.content.res.AssetManager;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
- import com.tencent.mm.opensdk.modelmsg.WXImageObject;
- import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
- import com.tencent.mm.opensdk.modelmsg.WXTextObject;
- import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
- import com.tencent.mm.opensdk.openapi.IWXAPI;
- import java.io.IOException;
- import java.io.InputStream;
- /**
- * 分享功能
- */
- public class ShareUtils {
- //微信朋友
- public static final int WX_SESSION = 1;
- //微信朋友圈
- public static final int WX_TIME_LINE = 2;
- public static final int THUMB_SIZE = 150;
- private static String buildTransaction(final String type) {
- return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
- }
- /**
- * 从assets中读取图片
- */
- public static Bitmap getImageFromAssetsFile(Context context, String fileName) {
- Bitmap image = null;
- AssetManager am = context.getResources().getAssets();
- InputStream is = null;
- try {
- is = am.open(fileName);
- image = BitmapFactory.decodeStream(is);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (null != is) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return image;
- }
- public static void shareWXImage(IWXAPI api, Context context, String fileName, String thumbFileName) {
- // Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resId);
- Bitmap bmp = getImageFromAssetsFile(context, fileName);
- Bitmap thumbB = getImageFromAssetsFile(context, thumbFileName);
- shareWXImage(api, bmp, thumbB);
- }
- public static void shareWXImage(IWXAPI api, Bitmap bmp, Bitmap thumbB) {
- // Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resId);
- // Bitmap bmp = getImageFromAssetsFile(context, fileName);
- WXImageObject imgObj = new WXImageObject(bmp);
- WXMediaMessage msg = new WXMediaMessage();
- msg.mediaObject = imgObj;
- //设置缩略图(不能大于32k)
- // Bitmap thumbBmp =compressImage(bmp);
- // Bitmap thumbB = getImageFromAssetsFile(context, thumbFileName);
- Bitmap thumbBmp = Bitmap.createScaledBitmap(thumbB, THUMB_SIZE, THUMB_SIZE * thumbB.getHeight() / thumbB.getWidth(), true);
- if (bmp != null) {
- bmp.recycle();
- }
- msg.thumbData = ImageTools.bmpToByteArray(thumbBmp, false);
- SendMessageToWX.Req req = new SendMessageToWX.Req();
- //用于唯一标识请求
- req.transaction = buildTransaction("img");
- req.message = msg;
- req.scene = SendMessageToWX.Req.WXSceneSession;
- api.sendReq(req);
- }
- public static void shareWXText(IWXAPI api, String text) {
- WXTextObject textObject = new WXTextObject(text);
- WXMediaMessage msg = new WXMediaMessage();
- msg.mediaObject = textObject;
- msg.description = text;
- SendMessageToWX.Req req = new SendMessageToWX.Req();
- //用于唯一标识请求
- req.transaction = buildTransaction("text");
- req.message = msg;
- req.scene = SendMessageToWX.Req.WXSceneSession;
- api.sendReq(req);
- }
- public static void shareWXWebpage(IWXAPI api, Context context, String bitmapName, String url, String title, String desc) {
- WXWebpageObject webpage = new WXWebpageObject();
- webpage.webpageUrl = url;
- WXMediaMessage message = new WXMediaMessage(webpage);
- message.title = title;
- message.description = desc;
- Bitmap bitmap = getImageFromAssetsFile(context, bitmapName);
- message.thumbData = ImageTools.bmpToByteArray(bitmap, true);
- SendMessageToWX.Req req = new SendMessageToWX.Req();
- req.transaction = buildTransaction("webpage");
- req.message = message;
- req.scene = SendMessageToWX.Req.WXSceneSession;
- api.sendReq(req);
- }
- }
|