什么是 Android 系统的原生分享呢,如下图所示
创建一个 Intent ,指定其 Action 为 Intent.ACTION_SEND,这表示要创建一个发送指定内容的行动。
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
指定需要发送的内容和类型。
// 比如发送文本形式的数据内容
// 指定发送的内容
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text");
// 指定发送内容的类型
sendIntent.setType("text/plain");
// 比如发送二进制文件数据流内容(比如图片、视频、音频文件等等)
// 指定发送的内容 (EXTRA_STREAM 对于文件 Uri )
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
// 指定发送内容的类型 (MIME type)
shareIntent.setType("image/jpeg");
向系统发送Activity,打开系统分享选择器,出现如上图所示界面。
startActivity(Intent.createChooser(shareIntent, "Share"));
完整代码如下:
// 原生通用分享文本
public void shareText(String title, String text){
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
if (title.isEmpty()){
title = "share";
}
sendIntent.putExtra(Intent.EXTRA_TEXT, text);
sendIntent.setType("text/plain");
startActivityForResult(Intent.createChooser(sendIntent, title), 80001);
}
// 原生通用分享图片
public void shareImage(String title, String filePath){
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
if (title.isEmpty()){
title = "share";
}
File file = new File(filePath);
Uri uri = getFileUri(this, file);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("image/png");
startActivityForResult(Intent.createChooser(sendIntent, title), 80002);
}
public Uri getFileUri(Context context, File file){
Uri uri;
// 低版本直接用 Uri.fromFile
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
uri = Uri.fromFile(file);
}else {
try {
uri = FileProvider.getUriForFile(this, getPackageName0() + ".fileProvider", file);
} catch (Exception e) {
e.printStackTrace();
uri = getImageContentUri(context, file);
}
if (uri == null) {
uri = getImageContentUri(context, file);
}
}
return uri;
}
public Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
如果要分享到指定的app呢?
// 特定App 分享文本
public void shareTextByApp(String pkgName, String appName, String title, String text){
if (!checkAppInstalled(this, pkgName))
{
Toast.makeText(getApplicationContext(), "You need install the " + appName + " first", Toast.LENGTH_LONG).show();
return;
}
if (title.isEmpty()){
title = "share";
}
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
sendIntent.putExtra(Intent.EXTRA_TEXT, text);
sendIntent.setType("text/plain");
sendIntent.setPackage(pkgName);
startActivity(sendIntent);
}
// 特定App 分享文本
public void shareImageByApp(String pkgName, String appName, String title, String filePath){
if (!checkAppInstalled(this, pkgName))
{
Toast.makeText(getApplicationContext(), "You need install the " + appName + " first", Toast.LENGTH_LONG).show();
return;
}
if (title.isEmpty()){
title = "share";
}
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
File file = new File(filePath);
Uri uri = getFileUri(this, file);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("image/png");
sendIntent.setPackage(pkgName);
startActivity(sendIntent);
}
// 是否安装某app
public boolean checkAppInstalled(Context context, String pkgName) {
try {
context.getPackageManager().getPackageInfo(pkgName, 0);
} catch (Exception x) {
return false;
}
return true;
}
//例如 WhatsApp 的分享
shareTextByApp("com.whatsapp", "WhatsApp", title, content);
//Facebook 的分享
shareImageByApp("com.facebook.katana", "Facebook", title, filePath);