admin
2022-08-09 399ac289f80b7a40aa4210341db6b447cacdcf14
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.tejia.lijin.app.util.share;
 
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.widget.Toast;
 
import java.util.ArrayList;
 
public class ShareImageUtil {
    /**
     * 根据intent分享图片
     *
     * @param context
     * @param imageList
     * @param packageName
     * @param activityName
     */
    private static void sharePictureByIntent(Context context, ArrayList<Uri> imageList, String packageName, String activityName) {
        Intent intent = new Intent();
        ComponentName comp = new ComponentName(packageName, activityName);
        intent.setComponent(comp);
        intent.setType("image/*");
        if (imageList.size() == 1) {
            intent.putExtra(Intent.EXTRA_STREAM, imageList.get(0));
            intent.setAction(Intent.ACTION_SEND);
        } else {
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageList);
            intent.setAction(Intent.ACTION_SEND_MULTIPLE);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(String.valueOf(Intent.CONTENTS_FILE_DESCRIPTOR), "banli");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);//增加读写权限
        }
        context.startActivity(intent);
//        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
//        context.startActivity(Intent.createChooser(intent, "分享到:"));
    }
 
 
    /**
     * 采用intent的方式分享到微信好友
     *
     * @param imageList
     */
    public static void shareWXPictureByIntent(Context context, ArrayList<Uri> imageList) {
        try {
            sharePictureByIntent(context, imageList, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
        } catch (Exception e) {
            Toast.makeText(context, "尚未安装微信", Toast.LENGTH_SHORT).show();
        }
    }
 
 
    /**
     * 分享到微信朋友圈
     *
     * @param context
     * @param imageList
     */
    public static void shareWXCirclePictureByIntent(Context context, ArrayList<Uri> imageList) {
        ArrayList<Uri> tempList = new ArrayList<>();
 
        if (imageList.size() > 0)
            tempList.add(imageList.get(0));
        try {
            sharePictureByIntent(context, tempList, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
        } catch (Exception e) {
            Toast.makeText(context, "尚未安装微信", Toast.LENGTH_SHORT).show();
        }
    }
 
 
    /**
     * 分享到QQ
     *
     * @param context
     * @param imageList
     */
    public static void shareQQPictureByIntent(Context context, ArrayList<Uri> imageList) {
        try {
            sharePictureByIntent(context, imageList, "com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
        } catch (Exception e) {
            Toast.makeText(context, "尚未安装QQ", Toast.LENGTH_SHORT).show();
        }
    }
 
 
    public static void shareQQZonePictureByIntent(Context context, ArrayList<Uri> imageList) {
        try {
            sharePictureByIntent(context, imageList, "com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
        } catch (Exception e) {
            Toast.makeText(context, "尚未安装QQ", Toast.LENGTH_SHORT).show();
        }
    }
 
 
    /**
     * 分享到新浪图片
     *
     * @param context
     * @param imageList
     */
    public static void shareSinaPictureByIntent(Context context, ArrayList<Uri> imageList) {
        try {
            sharePictureByIntent(context, imageList, "com.sina.weibo", "com.sina.weibo.sdk.web.WeiboSdkWebActivity");
        } catch (Exception e) {
            Toast.makeText(context, "尚未安装微博", Toast.LENGTH_SHORT).show();
        }
    }
}