Android 调用系统分享(不使用第三方),指定QQ、微信等

分享文本信息

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,"This is a text");
startActivity(Intent.createChooser(intent,"Share"));

分享单张图片

String path = Environment.getExternalStorageDirectory() + File.separator;//sd根目录
File file = new File(path, "test" + ".png");//这里test.png是sd卡根目录下的一个图片文件
Uri imageUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent, "Share"));

分享视频

String path = Environment.getExternalStorageDirectory() + File.separator;//sd根目录
File file = new File(path, "test" + ".mp4");
Uri audioUri = Uri.fromFile(file);
ComponentName comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
Intent shareIntent = new Intent();
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, audioUri);
shareIntent.setType("audio/*");
startActivity(Intent.createChooser(shareIntent, "分享视频"));

分享多个文件

String path = Environment.getExternalStorageDirectory() + File.separator;//sd根目录

ArrayList imageUris = new ArrayList<>();
File file = new File(path, "test" + ".png");
File file2 = new File(path, "test2" + ".png");
Uri imageUri = Uri.fromFile(file);
Uri imageUri2 = Uri.fromFile(file2);
imageUris.add(imageUri);
imageUris.add(imageUri2);

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);
startActivity(Intent.createChooser(intent, "Share"));

指定分享到微信朋友

ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
Intent shareIntent = new Intent();
shareIntent.setComponent(comp);            
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享多张图片"));

指定分享到微信朋友圈

ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
Intent shareIntent = new Intent();
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);   
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享多张图片"));

指定分享到QQ好友

ComponentName comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
Intent shareIntent = new Intent();
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);  
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享多张图片"));

视频指定分享到QQ空间

String path = Environment.getExternalStorageDirectory() + File.separator;//sd根目录
File file = new File(path, "test" + ".mp4");
Uri audioUri = Uri.fromFile(file);
ComponentName comp = new ComponentName("com.qzone", "com.qzonex.module.maxvideo.activity.QzonePublishVideoActivity");
Intent shareIntent = new Intent();
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, audioUri);
shareIntent.setType("video/*");
startActivity(Intent.createChooser(shareIntent, "分享视频"));

分享到指定程序

只要获得包名和相应Activity即可,方法可以查看以下文章

戳这里查看

你可能感兴趣的:(Android从入门到放弃)