安卓开发学习之利用intent实现微信分享

介绍

分享功能几乎是每个社交应用都要有的功能,其实现,既可以调用相关第三方sdk,也可以利用意图直接实现分享。不过意图方式,我的Android7.0手机只支持转发本地图片给朋友圈


实现

ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
Intent intent = new Intent(Intent.ACTION_SEND); // 设置intent的action
intent.setComponent(componentName); // 设置intent的目标activity名 前面是包名,后面是activity的完整名
intent.setType("image/*"); // 设置intent的type 
String path = Environment.getExternalStorageDirectory().getPath() + "/Pictures/dongqiudi/1523624189281.jpg";
File file = new File(path);
if (file.exists()) {
   Uri uri = FileProvider.getUriForFile(MainActivity.this, "com.example.songzeceng.myFileProvider", file); // 获取本地文件uri
   intent.putExtra(Intent.EXTRA_STREAM, uri); // 把uri存入intent,键名是固定值
}
intent.putExtra("Kdescrpition","测试转发一张图片"); // 存入说明文字,键名也是固定值
startActivity(intent); // 发送意图,进入微信的朋友圈分享界面

代码很简单,步骤清晰明了。只不过在android高版本中,获取本地文件uri必须要用FileProvider,关于FileProvider的用法,请参考我的这篇文章Android开发学习之FileProvider的使用


运行结果

直接上图



猜你喜欢

转载自blog.csdn.net/qq_37475168/article/details/80090411
今日推荐