d6c2ea3d77395dd063e5e4b880da997d38f0585f.svn-base 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.example.myapplication.utils;
  2. import android.content.Context;
  3. import android.content.res.AssetManager;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
  7. import com.tencent.mm.opensdk.modelmsg.WXImageObject;
  8. import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
  9. import com.tencent.mm.opensdk.modelmsg.WXTextObject;
  10. import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
  11. import com.tencent.mm.opensdk.openapi.IWXAPI;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. /**
  15. * 分享功能
  16. */
  17. public class ShareUtils {
  18. //微信朋友
  19. public static final int WX_SESSION = 1;
  20. //微信朋友圈
  21. public static final int WX_TIME_LINE = 2;
  22. public static final int THUMB_SIZE = 150;
  23. private static String buildTransaction(final String type) {
  24. return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
  25. }
  26. /**
  27. * 从assets中读取图片
  28. */
  29. public static Bitmap getImageFromAssetsFile(Context context, String fileName) {
  30. Bitmap image = null;
  31. AssetManager am = context.getResources().getAssets();
  32. InputStream is = null;
  33. try {
  34. is = am.open(fileName);
  35. image = BitmapFactory.decodeStream(is);
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. } finally {
  39. if (null != is) {
  40. try {
  41. is.close();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. return image;
  48. }
  49. public static void shareWXImage(IWXAPI api, Context context, String fileName, String thumbFileName) {
  50. // Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resId);
  51. Bitmap bmp = getImageFromAssetsFile(context, fileName);
  52. Bitmap thumbB = getImageFromAssetsFile(context, thumbFileName);
  53. shareWXImage(api, bmp, thumbB);
  54. }
  55. public static void shareWXImage(IWXAPI api, Bitmap bmp, Bitmap thumbB) {
  56. // Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resId);
  57. // Bitmap bmp = getImageFromAssetsFile(context, fileName);
  58. WXImageObject imgObj = new WXImageObject(bmp);
  59. WXMediaMessage msg = new WXMediaMessage();
  60. msg.mediaObject = imgObj;
  61. //设置缩略图(不能大于32k)
  62. // Bitmap thumbBmp =compressImage(bmp);
  63. // Bitmap thumbB = getImageFromAssetsFile(context, thumbFileName);
  64. Bitmap thumbBmp = Bitmap.createScaledBitmap(thumbB, THUMB_SIZE, THUMB_SIZE * thumbB.getHeight() / thumbB.getWidth(), true);
  65. if (bmp != null) {
  66. bmp.recycle();
  67. }
  68. msg.thumbData = ImageTools.bmpToByteArray(thumbBmp, false);
  69. SendMessageToWX.Req req = new SendMessageToWX.Req();
  70. //用于唯一标识请求
  71. req.transaction = buildTransaction("img");
  72. req.message = msg;
  73. req.scene = SendMessageToWX.Req.WXSceneSession;
  74. api.sendReq(req);
  75. }
  76. public static void shareWXText(IWXAPI api, String text) {
  77. WXTextObject textObject = new WXTextObject(text);
  78. WXMediaMessage msg = new WXMediaMessage();
  79. msg.mediaObject = textObject;
  80. msg.description = text;
  81. SendMessageToWX.Req req = new SendMessageToWX.Req();
  82. //用于唯一标识请求
  83. req.transaction = buildTransaction("text");
  84. req.message = msg;
  85. req.scene = SendMessageToWX.Req.WXSceneSession;
  86. api.sendReq(req);
  87. }
  88. public static void shareWXWebpage(IWXAPI api, Context context, String bitmapName, String url, String title, String desc) {
  89. WXWebpageObject webpage = new WXWebpageObject();
  90. webpage.webpageUrl = url;
  91. WXMediaMessage message = new WXMediaMessage(webpage);
  92. message.title = title;
  93. message.description = desc;
  94. Bitmap bitmap = getImageFromAssetsFile(context, bitmapName);
  95. message.thumbData = ImageTools.bmpToByteArray(bitmap, true);
  96. SendMessageToWX.Req req = new SendMessageToWX.Req();
  97. req.transaction = buildTransaction("webpage");
  98. req.message = message;
  99. req.scene = SendMessageToWX.Req.WXSceneSession;
  100. api.sendReq(req);
  101. }
  102. }