最近接一个小程序的分享,要求我们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 设置小一点就行了。
如果有什么疑问或建议欢迎留言或私信。