Android 微信分享圖片
阿新 • • 發佈:2017-11-21
static compress recycle 我們 text scale png _id string
private String APP_ID = "00000000000000000"; //微信 APPID private IWXAPI iwxapi;
private void regToWx() { iwxapi = WXAPIFactory.createWXAPI(context, APP_ID, true);//這裏context記得初始化 iwxapi.registerApp(APP_ID); }
IMServer.getDiskBitmap(IMServer.url);
這個是我寫的 一個從內存卡讀取照片的類.. 可根據自己需求更改
private voidwxShare() { Bitmap bp = IMServer.getDiskBitmap(IMServer.url); WXImageObject wxImageObject = new WXImageObject(bp); WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = wxImageObject; //設置縮略圖 Bitmap mBp = Bitmap.createScaledBitmap(bp, 120, 120, true); bp.recycle(); msg.thumbData= bmpToByteArray(mBp, true); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("img");// transaction字段用 req.message = msg; req.scene = SendMessageToWX.Req.WXSceneSession; iwxapi.sendReq(req); }
我先上代碼,我們看看上面的代碼..設置縮略圖那
官方給的 代碼是
msg.thumbData = Util.bmpToByteArray(thumbBitmap, true);
然後Util類,居然找不到(我用了qq的jar包,只能在這裏找到.....)
所以我只能去demo裏面找,然後把bmpToByteArray方法提取出來,如下
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, output); if (needRecycle) { bmp.recycle(); } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
然後再往下看,
req.transaction = buildTransaction("img");// transaction字段用
很明顯 後面的是一個方法, 官方也沒給出... 老方法 ,去demo裏面找,如下
private String buildTransaction(final String type) { return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis(); }
Android 微信分享圖片