android中单图分享:微信好友、QQ好友、微博、本地

qq 需要添加 lib 库:open_sdk_r6008_lite.jar

1
2
3
4
5
6
// qq
implementation files('../libs/open_sdk_r6008_lite.jar')
// 微博
implementation "com.sina.weibo.sdk:core:$WEIBO_VERSION"
// wechat
implementation "com.tencent.mm.opensdk:wechat-sdk-android-without-mta:$WECHAT_VERSION"

方案 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

public class Uposter {

// [Android 分享功能之 微信 图片分享 - 陈三哥 - CSDN博客](https://blog.csdn.net/brokge/article/details/51851625)
public static void shareWeChatFriend(Activity context, String path) {
new Thread(() -> {
// [java - exposed beyond app through ClipData.Item.getUri - Stack Overflow](https://stackoverflow.com/questions/48117511/exposed-beyond-app-through-clipdata-item-geturi)
Uri uri = FileProvider.getUriForFile(context.getApplicationContext(), context.getPackageName() + ".provider", new File(path));

Intent shareIntent = new Intent();
//发送图片到朋友圈
//ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
//发送图片给好友。
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/jpeg");
context.startActivity(Intent.createChooser(shareIntent, "分享图片"));
}).start();
}

// [QQ分享官方第三方接入(分享纯图片及截取view的bitmap图) - haibo_bear的博客 - CSDN博客](https://blog.csdn.net/haibo_bear/article/details/51321204)
public static void shareToQQFriend(Activity context, String path) {
new Thread(() -> {
Tencent tencent = Tencent.createInstance(QQ_APP_ID, context);
Bundle shareParams = new Bundle();
shareParams.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
shareParams.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, path);
shareParams.putString(QQShare.SHARE_TO_QQ_APP_NAME, context.getResources().getString(R.string.app_name));
shareParams.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE);
tencent.shareToQQ(context, shareParams, null);
}).start();
}

// [Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博 - 她叫我小渝的个人空间 - OSCHINA](https://my.oschina.net/u/1462828/blog/2086000)
public static void shareToWeiboFriend(Activity context, String path) {

new Thread(() -> {
Intent intent = new Intent();
intent.setPackage("com.sina.weibo");
intent.setAction(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = FileProvider.getUriForFile(context.getApplicationContext(), context.getPackageName() + ".provider", new File(path));
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/*"); //分享文件
context.startActivity(Intent.createChooser(intent, "分享"));
}).start();
}

public static void saveImage(Activity context, String path) {
// [How can I display images from a specific folder on android gallery - Stack Overflow](https://stackoverflow.com/questions/13418807/how-can-i-display-images-from-a-specific-folder-on-android-gallery)
class SingleMediaScanner implements MediaScannerConnection.MediaScannerConnectionClient {

private MediaScannerConnection mMs;
private File mFile;
private Context mContext;

private SingleMediaScanner(Context context, File f) {
mFile = f;
mContext = context;
mMs = new MediaScannerConnection(context, this);
mMs.connect();
}

public void onMediaScannerConnected() {
mMs.scanFile(mFile.getAbsolutePath(), null);
}

public void onScanCompleted(String path, Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
mContext.startActivity(intent);
mMs.disconnect();
}

}
View v = context.findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(v, "保存成功", Snackbar.LENGTH_LONG);
snackbar.setAction("点击查看", v1 -> {
// showGallery(context);
new SingleMediaScanner(context, new File(path));
});
snackbar.show();
}

private static void showGallery(Context context) {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("content://media/internal/images/media")));
}
}

调用方式

1
Uposter.saveImage(mContext, "/sotrage/0/a.jpg")

方案 2

或者通过面向对象的方式来实现:

1
2
3
4
5
6
7
8
9
public interface Share {
void init(Activity context);

boolean isInstall();

void execute(String path);

String getNotInstallTip();
}

其他类实现上面的接口,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class PosterShareWechat implements PosterShare {
private Activity context;

@Override
public void init(Activity context) {
this.context = context;
}


@Override
public boolean isInstall() {
return Uapp.isWechatAvilible(context);
}

@Override
public void execute(String path) {
// [Android 分享功能之 微信 图片分享 - 陈三哥 - CSDN博客](https://blog.csdn.net/brokge/article/details/51851625)
// [java - exposed beyond app through ClipData.Item.getUri - Stack Overflow](https://stackoverflow.com/questions/48117511/exposed-beyond-app-through-clipdata-item-geturi)
Uri uri = FileProvider.getUriForFile(context.getApplicationContext(), context.getPackageName() + ".provider", new File(path));

Intent shareIntent = new Intent();
//发送图片到朋友圈
//ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
//发送图片给好友。
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/jpeg");
context.startActivity(Intent.createChooser(shareIntent, "分享图片"));
}

@Override
public String getNotInstallTip() {
return "您没有安装微信";
}
}

调用方式

1
2
3
4
5
6
7
posterShare = new PosterShareWechat();
posterShare.init(context);
if (!posterShare.isInstall()) {
Utoast.show(posterShare.getNotInstallTip());
return;
}
posterShare.execute(path);