Android 使用系统自带分享功能

way1:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");// setType("audio/*");
    intent.putExtra(Intent.EXTRA_SUBJECT, "share");
    intent.putExtra(Intent.EXTRA_TEXT, "test http://www.qq.com/   测试");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(Intent.createChooser(intent, getTitle()));

way2:

bgimg0 = getImageFromAssetsFile("Cat_Blink/cat_blink0000.png");

/**
* 从Assets中读取图片
*/
private Bitmap getImageFromAssetsFile(String fileName)
{
Bitmap image = null;
AssetManager am = getResources().getAssets();
try
{
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}

    return image;

}

上面的代码是从assets中获取图片的代码,下面的代码是分享图片的代码:

/**
* 分享功能
*
* @param context
* 上下文
* @param activityTitle
* Activity的名字
* @param msgTitle
* 消息标题
* @param msgText
* 消息内容
* @param imgPath
* 图片路径,不分享图片则传null
*/
public void shareMsg(String activityTitle, String msgTitle, String msgText,
String imgPath) {
Intent intent = new Intent(Intent.ACTION_SEND);
if (imgPath == null || imgPath.equals("")) {
intent.setType("text/plain"); // 纯文本
} else {
File f = new File(imgPath);
if (f != null && f.exists() && f.isFile()) {
intent.setType("image/jpg");
Uri u = Uri.fromFile(f);
intent.putExtra(Intent.EXTRA_STREAM, u);
}
}
intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
intent.putExtra(Intent.EXTRA_TEXT, msgText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, activityTitle));
}

原文地址:https://www.cnblogs.com/wgscd/p/6612479.html