欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自帶API實(shí)現(xiàn)分享功能

 更新時(shí)間:2018年04月25日 16:31:16   作者:夏沫凡塵  
這篇文章主要為大家詳細(xì)介紹了Android自帶API實(shí)現(xiàn)分享功能,實(shí)現(xiàn)文字和圖片的分享,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

在做項(xiàng)目的過(guò)程中需要實(shí)現(xiàn)文字和圖片的分享,有兩種方式:
1. 使用android sdk中自帶的Intent.ACTION_SEND實(shí)現(xiàn)分享。
2. 使用shareSDK、友盟等第三方的服務(wù)。
鑒于使用的方便,此次只介紹使用Android sdk中自帶的方式來(lái)實(shí)現(xiàn)分享的功能。

分享文字

/** 
   * 分享文字內(nèi)容 
   * 
   * @param dlgTitle 
   *      分享對(duì)話(huà)框標(biāo)題 
   * @param subject 
   *      主題 
   * @param content 
   *      分享內(nèi)容(文字) 
   */ 
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); 

    // 設(shè)置彈出框標(biāo)題 
    if (dlgTitle != null && !"".equals(dlgTitle)) { // 自定義標(biāo)題 
      startActivity(Intent.createChooser(intent, dlgTitle)); 
    } else { // 系統(tǒng)默認(rèn)標(biāo)題 
      startActivity(intent); 
    } 
  } 

分享單張圖片

/** 
   * 分享圖片和文字內(nèi)容 
   * 
   * @param dlgTitle 
   *      分享對(duì)話(huà)框標(biāo)題 
   * @param subject 
   *      主題 
   * @param content 
   *      分享內(nèi)容(文字) 
   * @param uri 
   *      圖片資源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); 
    } 

    // 設(shè)置彈出框標(biāo)題 
    if (dlgTitle != null && !"".equals(dlgTitle)) { // 自定義標(biāo)題 
      startActivity(Intent.createChooser(intent, dlgTitle)); 
    } else { // 系統(tǒng)默認(rèn)標(biāo)題 
      startActivity(intent); 
    } 
  } 

分享多張圖片

//分享多張圖片 
  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, "分享到")); 
  } 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論