首页 > 代码库 > 分享?ShareUtils

分享?ShareUtils

  1. public class ShareUtils {
  2. /**
  3. * 分享功能
  4. * @param context 上下文
  5. * @param activityTitle Activity的名字
  6. * @param msgTitle 消息标题
  7. * @param msgText 消息内容
  8. * @param imgPath 图片路径,不分享图片则传null
  9. */
  10. public static void shareMsg(Context context, String activityTitle, String msgTitle, String msgText, String imgPath) {
  11. Intent intent = new Intent(Intent.ACTION_SEND);
  12. if (imgPath == null || imgPath.equals("")) {
  13. intent.setType("text/plain"); // 纯文本
  14. } else {
  15. File f = new File(imgPath);
  16. if (f != null && f.exists() && f.isFile()) {
  17. intent.setType("image/png");
  18. Uri u = Uri.fromFile(f);
  19. intent.putExtra(Intent.EXTRA_STREAM, u);
  20. }
  21. }
  22. intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
  23. intent.putExtra(Intent.EXTRA_TEXT, msgText);
  24. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  25. context.startActivity(Intent.createChooser(intent, activityTitle));
  26. }
  27. }
  28. 分享图片
  29. public void shareImage(String imgPath){
  30. if(TextUtils.isEmpty(imgPath)){
  31. Toast.makeText(this,"Share failed",Toast.LENGTH_SHORT).show();
  32. finish();
  33. return;
  34. }
  35. Intent intent = new Intent(Intent.ACTION_SEND);
  36. File f = new File(imgPath);
  37. if (f != null && f.exists() && f.isFile()) {
  38. intent.setType("image/jpg");
  39. Uri u = Uri.fromFile(f);
  40. intent.putExtra(Intent.EXTRA_STREAM, u);
  41. try{
  42. boolean isCommonPackageName = CommonPackageName.isCommonPackageName(mImName,this);
  43. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  44. if(isCommonPackageName){
  45. intent.setPackage(mImName);
  46. startActivity(intent);
  47. }else{
  48. startActivity(Intent.createChooser(intent,"Share"));
  49. }
  50. if(TextUtils.isEmpty(mTag)){
  51. AnalyseEvent.LogEvent(StickerImageCustomActivity.this,AnalyseEvent.KEYBOARD,AnalyseEvent.STICKER, mStickerFrom + AnalyseEvent.STICKERSHARE+AnalyseEvent.STICKERSHARENOTTEXT+mStickerEditState+mStickerRemoveState+mStickerDefaultText);
  52. }else{
  53. AnalyseEvent.LogEvent(StickerImageCustomActivity.this,AnalyseEvent.KEYBOARD,AnalyseEvent.STICKER, mStickerFrom + AnalyseEvent.STICKERSHARE+AnalyseEvent.STICKERSHAREWithText + mStickerEditState+mStickerRemoveState+mStickerDefaultText);
  54. }
  55. }catch(Exception e){
  56. Toast.makeText(this,"Package not found!",Toast.LENGTH_SHORT).show();
  57. }
  58. }else{
  59. Toast.makeText(this,"Share failed",Toast.LENGTH_SHORT).show();
  60. }
  61. }



来自为知笔记(Wiz)


分享?ShareUtils