Android comes with API to realize sharing function

  • 2021-09-05 00:51:42
  • OfStack

Preface

There are two ways to share words and pictures in the process of doing projects:
1. Use Intent.ACTION_SEND that comes with android sdk for sharing.
2. Use third-party services such as shareSDK and Youmeng.
In view of the convenience of use, this time only introduces the way that comes with Android sdk to realize the sharing function.

Share text


/** 
   *  Share text content  
   * 
   * @param dlgTitle 
   *       Share dialog box title  
   * @param subject 
   *       Theme  
   * @param content 
   *       Share content (text)  
   */ 
private void shareText(String dlgTitle, String subject, String content) { 
    if (content == null || "".equals(content)) { 
      return; 
    } 
    Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.setType("text/plain"); 
    if (subject != null && !"".equals(subject)) { 
      intent.putExtra(Intent.EXTRA_SUBJECT, subject); 
    } 

    intent.putExtra(Intent.EXTRA_TEXT, content); 

    //  Set the pop-up box title  
    if (dlgTitle != null && !"".equals(dlgTitle)) { //  Custom Title  
      startActivity(Intent.createChooser(intent, dlgTitle)); 
    } else { //  System default title  
      startActivity(intent); 
    } 
  } 

Share a single picture


/** 
   *  Share pictures and text content  
   * 
   * @param dlgTitle 
   *       Share dialog box title  
   * @param subject 
   *       Theme  
   * @param content 
   *       Share content (text)  
   * @param uri 
   *       Picture resource URI 
   */ 
  private void shareImg(String dlgTitle, String subject, String content, 
      Uri uri) { 
    if (uri == null) { 
      return; 
    } 
    Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.setType("image/*"); 
    intent.putExtra(Intent.EXTRA_STREAM, uri); 
    if (subject != null && !"".equals(subject)) { 
      intent.putExtra(Intent.EXTRA_SUBJECT, subject); 
    } 
    if (content != null && !"".equals(content)) { 
      intent.putExtra(Intent.EXTRA_TEXT, content); 
    } 

    //  Set the pop-up box title  
    if (dlgTitle != null && !"".equals(dlgTitle)) { //  Custom Title  
      startActivity(Intent.createChooser(intent, dlgTitle)); 
    } else { //  System default title  
      startActivity(intent); 
    } 
  } 

Share multiple pictures


// Share multiple pictures  
  public void shareMultipleImage(View view) { 
    ArrayList<Uri> uriList = new ArrayList<>(); 

    String path = Environment.getExternalStorageDirectory() + File.separator; 
    uriList.add(Uri.fromFile(new File(path+"australia_1.jpg"))); 
    uriList.add(Uri.fromFile(new File(path+"australia_2.jpg"))); 
    uriList.add(Uri.fromFile(new File(path+"australia_3.jpg"))); 

    Intent shareIntent = new Intent(); 
    shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); 
    shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList); 
    shareIntent.setType("image/*"); 
    startActivity(Intent.createChooser(shareIntent, " Share to ")); 
  } 


Related articles: