随着微信愈来愈火,愈来愈多的应用要求有分享到微信的功能。虽然有不少平台都帮集成有分享功能,好比友盟。可是我的以为友盟集成的东西太多了,本身封装得太过度了,不少资源文件也要带进去,因此感受不是怎么好,因此本身也研究了一下微信的调用其SDK分享。下面说说步骤。android
第一:下载官方的sdk demo。微信
下载地址:http://open.weixin.qq.com/download/?lang=zh_CNeclipse
第二:解压,并将工程导入到eclipse测试
解压出来的时候,发现根目录下有一个debug.keystore文件,这个文件很关键的哦。ui
而后咱们运行看看,你会发现分享根本就不成功,是微信缘由吗,固然不是。spa
第三:在上面说到项目的根目录下有一个debug.keystore文件,由于咱们编译、签名apk的时候,用的是咱们自带的那个 debug.keystore,每台电脑都是不同的签名文件,并且微信那个APP_ID已经签名文件debug.keystore绑定了的,因此为何 咱们直接运行时候是不成功的。debug
解决方法就是将微信的那个debug.keystore拷贝到咱们电脑默认的那个debug.keystore位置,将其覆盖(建议先备份)。code
在window系统,这个签名文件在c:\用户\你的用户名\.android目录下(注意.android文件夹默认是隐藏的)。component
再次运行,分享就成功了。blog
若是是咱们的应用,将APP_ID替换成咱们在官网上面申请的APP_ID就好了。
其实咱们分享信息到微信,还有一种更简单的方法,不用其提供的SDK API,直接调用微信相关的Activity,这样更加省事,例如:
/** * 分享信息到朋友 * * @param file,假如图片的路径为path,那么file = new File(path); */ private void shareToFriend(File file) { Intent intent = new Intent(); ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI"); intent.setComponent(componentName); intent.setAction(Intent.ACTION_SEND); intent.setType("image/*"); intent.putExtra(Intent.EXTRA_TEXT, "测试微信"); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); startActivity(intent); }
/** * 分享信息到朋友圈 * * @param file,假如图片的路径为path,那么file = new File(path); */ private void shareToTimeLine(File file) { Intent intent = new Intent(); ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI"); intent.setComponent(componentName); intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); // intent.setAction(android.content.Intent.ACTION_SEND_MULTIPLE); // ArrayList<Uri> uris = new ArrayList<Uri>(); // for (int i = 0; i < images.size(); i++) { // Uri data = Uri.fromFile(new File(thumbPaths.get(i))); // uris.add(data); // } // intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); intent.setType("image/*"); startActivity(intent); }