Android 微信分享图片!!!

开始创建应用,通过审核等 我就不说了..

首先下载,微信的SDK

然后将jar包导入项目  ,可参考微信开发文档,然后有文档了,为什么还要写这个文章?对吧 

我只能吐槽,写开发文档的人太懒了.,好多没写明白

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();
}

 
 

发布了13 篇原创文章 · 获赞 5 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/u013241923/article/details/53635236