微信分享图片 文字 Android原生分享图片文字 微信小程序分享

本文介绍如何在Android应用中实现将图片和文字原生分享到微信,包括Bitmap转换为byte数组以及调用微信官方API进行小程序分享的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近接一个小程序的分享,要求我们app端生成一个图片给用户用微信扫码,本地直接调微信分享小程序链接。根据微信官方api(https://open.weixin.qq.com/)接了小程序,代码如下

 //小程序分享
public void shareLocal() {
     WXMiniProgramObject programObject = new WXMiniProgramObject();
     programObject.webpageUrl = "http://qq.com";
    //你申请的username和path
     programObject.userName = "sssssss";
     programObject.path = "22222222";
     WXMediaMessage mes = new WXMediaMessage(programObject);
     mes.title = "小程序分享";
     mes.description = "扫码打开小程序";
     ContentResolver resolver = getContentResolver();
     try {
         Bitmap bitmap = BitmapFactory.decodeStream(resolver
                 .openInputStream(uriLocal));
         Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true);
         bitmap.recycle();
         mes.thumbData = MyCommonUserUtils.Bitmap2Bytes(scaledBitmap);
         SendMessageToWX.Req req = new SendMessageToWX.Req();
         req.transaction = buildTransaction("webpage");
         req.message = mes;
         req.scene = SendMessageToWX.Req.WXSceneSession;
         IWXAPI api = WXAPIFactory.createWXAPI(MicroshopQRCodeActivity.this, "wxc25ceaf2882bcf67", false);
         int wxAppSupportAPI = api.getWXAppSupportAPI();
         api.sendReq(req);
     } catch (Exception e) {
         e.printStackTrace();
         LogUtil.e("========================" + e);

     }


 }
最后分享的时候却报了一个 无法分享到微信 由于应用和小程序未绑定在同一微信开放平台,无法分享到微信。
我们的用户全绑定到我们不太可能,所以只能通过图片分享了。
一开始用安卓原生的分享
代码如下: 
    //Android原生分享
    private void shareMultiplePictureToTimeLine(File... files) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        //"com.tencent.mm.ui.tools.ShareImgUI"发送给联系人
        //"com.tencent.mm","com.tencent.mm.ui.tools.AddFavoriteUI"添加到收藏
        //"com.tencent.mm.ui.tools.ShareToTimeLineUI" 发送到朋友圈
	//如要实现多平台分享,setComponent去掉即可。
        ComponentName comp = new ComponentName("com.tencent.mm",
                "com.tencent.mm.ui.tools.ShareImgUI");
        intent.setComponent(comp);
	//intent.setType("text/plain");

        intent.setType("image/*");
        try {
            File file = new File(new URI(uriLocal.toString()));
            Uri uri = Uri.fromFile(file);
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
            intent.putExtra(Intent.EXTRA_TEXT, "你好 ");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            startActivity(intent);
            startActivity(Intent.createChooser(intent, "我是标题"));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
发现无法实现图文分享要么分享文字要么分享图片,而且分享的图片时文件模式的,小图标也看不到。
后选择微信skd的图文分享
代码如下
//微信图片分享
    void shareByWx() {
        if (uriLocal == null) {
            MyCommonUserUtils.showSafeToast(MicroshopQRCodeActivity.this,"无可分享二维码!");
            return;
        }
        ContentResolver resolver = getContentResolver();
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(resolver
                    .openInputStream(uriLocal));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ;
        WXImageObject imageObject = new WXImageObject(bitmap);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imageObject;
        //设置缩略图
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true);
        bitmap.recycle();
        msg.thumbData = MyCommonUserUtils.bmpToByteArray(scaledBitmap, true);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        IWXAPI api = WXAPIFactory.createWXAPI(MicroshopQRCodeActivity.this, "wxc25ceaf2882bcf67", false);
//        如果未注册要先注册
//        api.registerApp("wxc25ceaf2882bcf67")
//        检查api是否有效
//        api.openWXApp();注意sdk版本
        api.sendReq(req);


    }
  
//saveImageFile为本地分享图片地址路径
    uriLocal = Uri.fromFile(saveImageFile);
补充一个bitmap 转byte 数组方法
 public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {

//        复制代码
        int i;
        int j;
        if (bmp.getHeight() > bmp.getWidth()) {
            i = bmp.getWidth();
            j = bmp.getWidth();
        } else {
            i = bmp.getHeight();
            j = bmp.getHeight();
        }

        Bitmap localBitmap = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565);
        Canvas localCanvas = new Canvas(localBitmap);

        while (true) {
            localCanvas.drawBitmap(bmp, new Rect(0, 0, i, j), new Rect(0, 0,i, j), null);
            if (needRecycle)
                bmp.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 = bmp.getHeight();
            j = bmp.getHeight();
        }
    }
最后遇到一个坑是无论怎么都跳不到微信分享页面
报错checkArgs fail, thumbData is invalid
查资料发现分享图片缩略图不能大于32kb
这里将
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true);
THUMB_SIZE 设置小一点就行了。
如果有什么疑问或建议欢迎留言或私信。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值