微信分享網頁

public void sendReq(Context context, String text, Bitmap bmp) {
        String url = http://www.ABC.net;//收到分享的好友點擊信息會跳轉到這個地址去
        WXWebpageObject localWXWebpageObject = new WXWebpageObject();
        localWXWebpageObject.webpageUrl = url;
        WXMediaMessage localWXMediaMessage = new WXMediaMessage(
                localWXWebpageObject);
        localWXMediaMessage.title ="個人應用"//不能太長,不然微信會提示出錯。不過博主沒驗證過具體能輸入多長。
        localWXMediaMessage.description = text;
        localWXMediaMessage.thumbData = getBitmapBytes(bmp, false);
        SendMessageToWX.Req localReq = new SendMessageToWX.Req();
        localReq.transaction = System.currentTimeMillis() + "";
        localReq.message = localWXMediaMessage;
        IWXAPI api = WXAPIFactory.createWXAPI(context, APP_ID, true);
        api.sendReq(localReq);
    }
// 須要對圖片進行處理,不然微信會在log中輸出thumbData檢查錯誤
    private static byte[] getBitmapBytes(Bitmap bitmap, boolean paramBoolean) {
        Bitmap localBitmap = Bitmap.createBitmap(80, 80, Bitmap.Config.RGB_565);
        Canvas localCanvas = new Canvas(localBitmap);
        int i;
        int j;
        if (bitmap.getHeight() > bitmap.getWidth()) {
            i = bitmap.getWidth();
            j = bitmap.getWidth();
        } else {
            i = bitmap.getHeight();
            j = bitmap.getHeight();
        }
        while (true) {
            localCanvas.drawBitmap(bitmap, new Rect(0, 0, i, j), new Rect(0, 0,
, 80), null);
            if (paramBoolean)
                bitmap.recycle();
            ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
            localBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
                    localByteArrayOutputStream);
            localBitmap.recycle();
            byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
            try {
                localByteArrayOutputStream.close();
                return arrayOfByte;
            } catch (Exception e) {
                F.out(e);
            }
            i = bitmap.getHeight();
            j = bitmap.getHeight();
        }
    }web

相關文章
相關標籤/搜索