Android系统分享的注册和调起

系统相册照片长按时会弹出分享发送选项,可以选择分享到QQ,微信等,本文主要介绍:

  1. 如何将自己的应用注册到系统分享中,使用户可以将照片视频文件等发送到自己的应用中。
  2. 如何接受解析系统分享发送过来的数据。
  3. 如何唤醒系统分享,分享照片视频文件到别的应用。

注册系统的分享

需要在 AndroidManifest.xml 文件声明 <intent-filter>,使得你可以在用户点击分享/发送按钮时调起你的应用,将图片和文字等分享到你的App。

关于 mimeType 的相关类型,请查看 MimeUtils.java 文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<activity android:name=".activity.HandleShareActivity">
<intent-filter>
<!--接受单个文件分享-->
<action android:name="android.intent.action.SEND"/>
<!--接受多个文件分享-->
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<!--接受图片类文件分享 image/jpeg" "image/bmp" "image/gif" "image/jpg" "image/png"-->
<data android:mimeType="image/*"/>
<!--接受文本分享 text/plain-->
<data android:mimeType="text/*"/>
<!--接受视频分享 video/wav video/mp4-->
<data android:mimeType="video/*"/>
<!--接受声音文件分享-->
<data android:mimeType="audio/*"/>
<!--接受压缩文件和其他各种文件分享-->
<data android:mimeType="application/*"/>
</intent-filter>
</activity>

接受分享的数据

当直接长按选中某些文本分享时拿到的数据有些不一样,是存储在 Intent.EXTRA_TEXT 中的,因此单独处理一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 处理分享页面发来的intent
*/
public static void handleIntent(Activity context) {
Intent intent = context.getIntent();
String type = intent.getType();
if (type == null)
return;
// 当文件格式无法识别或选择了多种类型的文件时,type会变成 */*
LogUtils.e("type " + type);
//type可能为的类型是 image/* | video/* | audio/* | text/* | application/* | */*
if (type.startsWith("text") && intent.getStringExtra(Intent.EXTRA_TEXT) != null) {
// 当直接选中文本分享时,会存放在EXTRA_TEXT里面,选择文本文件时,仍然存放在EXTRA_STREAM里面
LogUtils.e("获取到分享的文本 " + intent.getStringExtra(Intent.EXTRA_TEXT));
} else {
List<String> sharePaths = getSharePaths(context, intent);
LogUtils.e("获取到分享的文件的路径 " + sharePaths.toString());
}
}

区分多个文件和单个文件分享,获取分享过来的路径列表

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
/**
* 获取分享过来的路径
*
* @param context 上下文
* @param intent intent
* @return 路径列表
*/
private static List<String> getSharePaths(Context context, Intent intent) {
String action = intent.getAction();
List<String> paths = new ArrayList<>();
// 单个文件
if (Intent.ACTION_SEND.equals(action)) {
Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
String realPathFromURI = getRealPathFromURI(context, imageUri);
if (realPathFromURI != null)
paths.add(realPathFromURI);
}
}
// 多个文件
else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
for (Uri uri : imageUris) {
paths.add(getRealPathFromURI(context, uri));
}
}
}
return paths;
}

如何从 uri 中获取存储路径,将 uri 进行转换。

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
/**
* 从uri获取path
*
* @param uri uri
* @return path
*/
private static String getRealPathFromURI(Context context, Uri uri) {
final String scheme = uri.getScheme();
String data = null;
if (ContentResolver.SCHEME_FILE.equals(scheme)) {
data = uri.getPath();
} else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
Cursor cursor = context.getContentResolver()
.query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}
, null, null, null);
if (null != cursor) {
if (cursor.moveToFirst()) {
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
if (index > -1) {
data = cursor.getString(index);
}
}
cursor.close();
}
}
return data;
}

唤醒系统分享

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
/**
* 分享文字
*
* @param context 上下文
* @param title 文字标题
* @param content 文字内容
*/
public static void shareText(Context context, String title, String content) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, content);
shareIntent.putExtra(Intent.EXTRA_TITLE, title);
shareIntent.setType("text/plain");
//设置分享列表的标题,并且每次都显示分享列表
context.startActivity(Intent.createChooser(shareIntent, "分享到"));
}


/**
* 分享单张图片
*
* @param context 上下文
* @param path 图片的路径
*/
public static void shareImage(Context context, String path) {
//由文件得到uri
Uri imageUri = Uri.fromFile(new File(path));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
context.startActivity(Intent.createChooser(shareIntent, "分享到"));
}


/**
* 分享多张图片
*
* @param context 上下文
* @param paths 路径的集合
*/
public static void shareImages(Context context, List<String> paths) {
ArrayList<Uri> uriList = new ArrayList<>();
for (String path : paths) {
uriList.add(Uri.fromFile(new File(path)));
}
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
shareIntent.setType("image/*");
context.startActivity(Intent.createChooser(shareIntent, "分享到"));
}

附源码

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* CreateAt : 16/8/13
* Describe : 唤醒系统分享
*
* @author chendong
*/
public class ShareUtils {

/**
* 分享文字
*
* @param context 上下文
* @param title 文字标题
* @param content 文字内容
*/
public static void shareText(Context context, String title, String content) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, content);
shareIntent.putExtra(Intent.EXTRA_TITLE, title);
shareIntent.setType("text/plain");
//设置分享列表的标题,并且每次都显示分享列表
context.startActivity(Intent.createChooser(shareIntent, "分享到"));
}

/**
* 分享单张图片
*
* @param context 上下文
* @param path 图片的路径
*/
public static void shareImage(Context context, String path) {
//由文件得到uri
Uri imageUri = Uri.fromFile(new File(path));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
context.startActivity(Intent.createChooser(shareIntent, "分享到"));
}

/**
* 分享多张图片
*
* @param context 上下文
* @param paths 路径的集合
*/
public static void shareImages(Context context, List<String> paths) {
ArrayList<Uri> uriList = new ArrayList<>();
for (String path : paths) {
uriList.add(Uri.fromFile(new File(path)));
}
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
shareIntent.setType("image/*");
context.startActivity(Intent.createChooser(shareIntent, "分享到"));
}


/**
* 处理分享页面发来的intent
*/
public static void handleIntent(Activity context) {
Intent intent = context.getIntent();
String type = intent.getType();
if (type == null)
return;
// 当文件格式无法识别或选择了多种类型的文件时,type会变成 */*
LogUtils.e("type " + type);
//type可能为的类型是 image/* | video/* | audio/* | text/* | application/*
if (type.startsWith("text") && intent.getStringExtra(Intent.EXTRA_TEXT) != null) {
// 当直接选中文本分享时,会存放在EXTRA_TEXT里面,选择文本文件时,仍然存放在EXTRA_STREAM里面
LogUtils.e("获取到分享的文本 " + intent.getStringExtra(Intent.EXTRA_TEXT));
} else {
List<String> sharePaths = getSharePaths(context, intent);
LogUtils.e("获取到分享的文件的路径 " + sharePaths.toString());
}
}


/**
* 获取分享过来的路径
*
* @param context 上下文
* @param intent intent
* @return 路径列表
*/
private static List<String> getSharePaths(Context context, Intent intent) {
String action = intent.getAction();
List<String> paths = new ArrayList<>();
// 单个文件
if (Intent.ACTION_SEND.equals(action)) {
Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
String realPathFromURI = getRealPathFromURI(context, imageUri);
if (realPathFromURI != null)
paths.add(realPathFromURI);
}
}
// 多个文件
else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
for (Uri uri : imageUris) {
paths.add(getRealPathFromURI(context, uri));
}
}
}
return paths;
}


/**
* 从uri获取path
*
* @param uri uri
* @return path
*/
private static String getRealPathFromURI(Context context, Uri uri) {
final String scheme = uri.getScheme();
String data = null;
if (ContentResolver.SCHEME_FILE.equals(scheme)) {
data = uri.getPath();
} else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
Cursor cursor = context.getContentResolver()
.query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}
, null, null, null);
if (null != cursor) {
if (cursor.moveToFirst()) {
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
if (index > -1) {
data = cursor.getString(index);
}
}
cursor.close();
}
}
return data;
}
}
------ 本文结束 🎉🎉 谢谢观看  ------