读书人

Android 怎么将图片和文字同时分享到微

发布时间: 2013-08-13 16:43:28 作者: rapoo

Android 如何将图片和文字同时分享到微信
View Row Code

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();        }    }转载自:http://mobile.9sssd.com/android/art/1059

其实就是分享一张网页,同时提供该页面的缩略图、描述等信息啦。刚好可以拿来做同时分享图片和文字内容。其它部分代码就跟微信开放平台上提供的例子一样了。希望对需要的朋友有点帮助。

读书人网 >Android

热点推荐