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 void wxShare() {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(); }
?