Android系统是自带分享功能
(2023-09-24 02:12:48)分类: androidios |
简介
Android系统是自带分享功能的,不过也有一定的局限性,可以分享图片,文字,视频,音频等,也可以分享多图,但是不支持直接分享一个卡片(包括图文,链接),所以一般都是将需要分享的内容添加到图片中,或者通过整串文字的方式来分享。
下边是几种分享方式的具体代码:
action设为send或者send multiple,然后设置分享的类型和要分享内容
分享文字
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "文本内容");
intent.setType("text/plain");
startActivity(intent);
1
2
3
4
5
分享图片
private void shareImage(Bitmap bitmap) {
Intent
intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
Uri uri =
Uri.parse(MediaStore.Images.Media.insertImage(mActivity.getContentResolver(),
bitmap, "IMG" + Calendar.getInstance().getTime(), null));
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, uri);
mActivity.startActivity(Intent.createChooser(intent,
"title"));
}
1
2
3
4
5
6
7
8
分享多图
ArrayList imageUris = new ArrayList<>();
imageUris.add(uri);
imageUris.add(uri);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,
imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "dlgTitle"));
1
2
3
4
5
6
7
8
分享到指定平台
Intent wechatIntent = new Intent(Intent.ACTION_SEND);
wechatIntent.setPackage("com.tencent.mm");
wechatIntent.setType("text/plain");
wechatIntent.putExtra(Intent.EXTRA_TEXT, "分享到微信的内容");
startActivity(wechatIntent);
1
2
3
4
5
常用的就是这几种方式,下边是支持的分享类型:
text/plain
application/*
image/*
video/*
audio/*
1
2
3
4
5
项目中案例
fun shareImage(context:
Context?, bitmap: Bitmap?, isCircle: Boolean = false) {
val intent = Intent()
intent.action = Intent.ACTION_SEND
val uri = Uri.parse(
MediaStore.Images.Media.insertImage(
context?.contentResolver,
bitmap,
"IMG" + Calendar.getInstance().time,
null
)
)
intent.setPackage("com.tencent.mm")
intent.type = "image/*"
if (isCircle) {
Android系统是自带分享功能的,不过也有一定的局限性,可以分享图片,文字,视频,音频等,也可以分享多图,但是不支持直接分享一个卡片(包括图文,链接),所以一般都是将需要分享的内容添加到图片中,或者通过整串文字的方式来分享。
下边是几种分享方式的具体代码:
action设为send或者send multiple,然后设置分享的类型和要分享内容
分享文字
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "文本内容");
intent.setType("text/plain");
startActivity(intent);
分享图片
private void shareImage(Bitmap bitmap) {
}
分享多图
ArrayList imageUris = new ArrayList<>();
imageUris.add(uri);
imageUris.add(uri);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListEx
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "dlgTitle"));
分享到指定平台
Intent wechatIntent = new Intent(Intent.ACTION_SEND);
wechatIntent.setPackage("com.tencent.mm");
wechatIntent.setType("text/plain");
wechatIntent.putExtra(Intent.EXTRA_TEXT, "分享到微信的内容");
startActivity(wechatIntent);
常用的就是这几种方式,下边是支持的分享类型:
text/plain
application/*
image/*
video/*
audio/*
项目中案例