首页 > 代码库 > 分享?ShareUtils
分享?ShareUtils
public class ShareUtils {
/**
* 分享功能
* @param context 上下文
* @param activityTitle Activity的名字
* @param msgTitle 消息标题
* @param msgText 消息内容
* @param imgPath 图片路径,不分享图片则传null
*/
public static void shareMsg(Context context, 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/png");
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);
context.startActivity(Intent.createChooser(intent, activityTitle));
}
}
分享图片
public void shareImage(String imgPath){
if(TextUtils.isEmpty(imgPath)){
Toast.makeText(this,"Share failed",Toast.LENGTH_SHORT).show();
finish();
return;
}
Intent intent = new Intent(Intent.ACTION_SEND);
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);
try{
boolean isCommonPackageName = CommonPackageName.isCommonPackageName(mImName,this);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if(isCommonPackageName){
intent.setPackage(mImName);
startActivity(intent);
}else{
startActivity(Intent.createChooser(intent,"Share"));
}
if(TextUtils.isEmpty(mTag)){
AnalyseEvent.LogEvent(StickerImageCustomActivity.this,AnalyseEvent.KEYBOARD,AnalyseEvent.STICKER, mStickerFrom + AnalyseEvent.STICKERSHARE+AnalyseEvent.STICKERSHARENOTTEXT+mStickerEditState+mStickerRemoveState+mStickerDefaultText);
}else{
AnalyseEvent.LogEvent(StickerImageCustomActivity.this,AnalyseEvent.KEYBOARD,AnalyseEvent.STICKER, mStickerFrom + AnalyseEvent.STICKERSHARE+AnalyseEvent.STICKERSHAREWithText + mStickerEditState+mStickerRemoveState+mStickerDefaultText);
}
}catch(Exception e){
Toast.makeText(this,"Package not found!",Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(this,"Share failed",Toast.LENGTH_SHORT).show();
}
}
来自为知笔记(Wiz)
分享?ShareUtils
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。