Android 不调用SDK实现一键分享图片文字到微信等可选app


Android 不调用SDK实现一键分享图片文字到微信等可选app

  1. 实现对微信好友圈的分享:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void shareToWechatFriends() {
String imgPath = "/sdcard/share_pic.jpg";
Intent intent = new Intent(Intent.ACTION_SEND);
File file = new File(imgPath);
if (file != null && file.exists() && file.isFile()) {
intent.setType("image/*");
Uri u = Uri.fromFile(file);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
u = FileProvider.getUriForFile(this, this.getPackageName() + ".fileprovider", file);
}
intent.putExtra(Intent.EXTRA_STREAM, u);
}
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
intent.setComponent(comp);
intent.setAction("android.intent.action.SEND");
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_TEXT, "固定字段");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(intent);
}
  1. 实现对微信好友的分享:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void shareToWechat() {
String imgPath = "/sdcard/share_pic.jpg";
Intent intent = new Intent(Intent.ACTION_SEND);
File file = new File(imgPath);
if (file != null && file.exists() && file.isFile()) {
intent.setType("image/*");
Uri u = Uri.fromFile(file);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
u = FileProvider.getUriForFile(this, this.getPackageName() + ".fileprovider", file);
}
intent.putExtra(Intent.EXTRA_STREAM, u);
}
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
intent.setComponent(comp);
intent.setAction("android.intent.action.SEND");
intent.setType("image/*");
// intent.setFlags(0x3000001);
intent.putExtra(Intent.EXTRA_TEXT, "固定字段");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(intent);
}

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>

provider_paths.xml

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path path="." name="root" />
<files-path path="files/" name="files" />
</paths>