2011-07-25 46 views
47

我需要開發一個具有共享功能的應用程序。我必須分享Facebook,Twitter,電子郵件和其他服務。Android - 分享到Facebook,Twitter,郵件,ecc

我該怎麼做?網上有一個圖書館?對於iOS開發有ShareKit,但對於Android?

謝謝:)

+0

這取決於。你想分享什麼?文本?圖片?或者是什麼? – iTurki

+0

我需要發佈點擊圖片與分享貼文捕捉屏幕視圖和關於我需要分享的應用程序鏈接 – Harsha

回答

67

帕雷什Mayani的答覆中提到主要是正確的。只需使用廣播意圖讓系統和所有其他應用程序選擇共享內容的方式。

要共享文本使用下面的代碼:

String message = "Text I want to share."; 
Intent share = new Intent(Intent.ACTION_SEND); 
share.setType("text/plain"); 
share.putExtra(Intent.EXTRA_TEXT, message); 

startActivity(Intent.createChooser(share, "Title of the dialog the system will open")); 
+0

好;)謝謝:) –

+0

@Janusz:沒有任何方法可以直接獲得Facebook分享?我的意思是不允許系統打開名稱列表(臉書,電子郵件,推特等)以選擇特定名稱 – 2012-12-30 16:29:58

+0

是的,您可以整合Facebook SDK並直接使用共享選項。我不會推薦它。對於這5條線而言,結果會更糟糕,這需要很長時間。 – Janusz

26

我想你想給共享按鈕,點擊它合適的媒體/網站應該有這個選項與它分享。在Android中,您需要爲此創建createChooser

共享文本:

Intent sharingIntent = new Intent(Intent.ACTION_SEND); 
sharingIntent.setType("text/plain"); 
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is the text that will be shared."); 
startActivity(Intent.createChooser(sharingIntent,"Share using")); 

共享二進制對象(圖片,視頻等)

Intent sharingIntent = new Intent(Intent.ACTION_SEND); 
Uri screenshotUri = Uri.parse(path); 

sharingIntent.setType("image/png"); 
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri); 
startActivity(Intent.createChooser(sharingIntent, "Share image using")); 

僅供參考,上面的代碼是從Sharing content in Android using ACTION_SEND Intent

+1

您的共享文本示例共享一張圖片 – Janusz

+0

@Janusz這是錯誤的,順便說一句,我已經糾正它。 –

+1

現在你正在分享一個網頁:) – Janusz

1

ACTION_SEND只會給你選擇使用Gmail,YahooMail ...等發送(在手機上安裝任何應用程序,可以執行ACTION_SEND)。如果您想在Facebook或Twitter上分享,您需要爲每個按鈕分配自定義按鈕,並使用自己的SDK,如Facebook SDKTwitter4J

+3

這是不正確的... ACTION_SEND會調出設備上的任何東西。如果Facebook和Twitter在設備上,那麼無論註冊了ACTION_SEND,它們都會以共享選項 – taraloca

+1

的形式出現,並會創建一個選擇器。無法使用action_send直接訪問facebook的twitter。這就是我所說的。 –

+0

你總是可以編輯你的答案並澄清它。 – Janusz

3

ACTION_SEND將爲所有人正常工作,並且它將文本正文放在twitter,G郵箱中,但它對於Face Book頁面失敗......它作爲Facebook android SDK中的已知錯誤..但他們仍然沒有固定它

+1

使這個評論,我會upvote它。 – Brian

+4

反對 \t ACTION_SEND將爲所有人正常工作,它需要文本的正文在牆上的twitter,G郵件..但它失敗了對於臉書頁...它作爲已知的bug在Facebook的android SDK ..但他們還沒有修復它 – RAJESH

+0

'ACTION_SEND'適用於Facebook和Facebook Messenger。但是如果你想讓消息顯示的帖子顯示有意義的縮略圖和描述,那麼在服務器上準備適當的[OpenGraph meta-tags](https://developers.facebook.com/docs/plugins/checklistserver-side)是值得的。請參閱http:// stackoverflow。COM /問題/ 1138460 /如何,的確,Facebook的分享者,選擇圖像#19388031。 –

1

我認爲下面的代碼將有助於....

public void btnShareClick(View v) { 
    // shareBtnFlag = 1; 
    Dialog d = new Dialog(DrawAppActivity.this); 
    d.requestWindowFeature(d.getWindow().FEATURE_NO_TITLE); 
    d.setCancelable(true); 

    d.setContentView(R.layout.sharing); 

    final Button btnFacebook = (Button) d.findViewById(R.id.btnFacebook); 
    final Button btnEmail = (Button) d.findViewById(R.id.btnEmail); 

    btnEmail.setOnClickListener(new OnClickListener() { 

     public void onClick(View arg0) { 
      if (!btnEmail.isSelected()) { 
       btnEmail.setSelected(true); 
      } else { 
       btnEmail.setSelected(false); 
      } 
      saveBtnFlag = 1; 
      // Check if email id is available------------- 
      AccountManager manager = AccountManager 
        .get(DrawAppActivity.this); 
      Account[] accounts = manager.getAccountsByType("com.google"); 
      Account account = CommonFunctions.getAccount(manager); 
      if (account.name != null) { 
       emailSendingTask eTask = new emailSendingTask(); 
       eTask.execute(); 
       if (CommonFunctions.createDirIfNotExists(getResources() 
         .getString(R.string.path))) 

       { 
        tempImageSaving(
          getResources().getString(R.string.path), 
          getCurrentImage()); 
       } 

       Intent sendIntent; 
       sendIntent = new Intent(Intent.ACTION_SEND); 
       sendIntent.setType("application/octet-stream"); 
       sendIntent.setType("image/jpeg"); 
       sendIntent.putExtra(Intent.EXTRA_EMAIL, 
         new String[] { account.name }); 
       sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Drawing App"); 
       sendIntent.putExtra(Intent.EXTRA_TEXT, "Check This Image"); 
       sendIntent.putExtra(Intent.EXTRA_STREAM, 
         Uri.parse("file://" + tempPath.getPath())); 

       List<ResolveInfo> list = getPackageManager() 
         .queryIntentActivities(sendIntent, 
           PackageManager.MATCH_DEFAULT_ONLY); 

       if (list.size() != 0) { 

        startActivity(Intent.createChooser(sendIntent, 
          "Send Email Using:")); 

       } 

       else { 
        AlertDialog.Builder confirm = new AlertDialog.Builder(
          DrawAppActivity.this); 
        confirm.setTitle(R.string.app_name); 
        confirm.setMessage("No Email Sending App Available"); 
        confirm.setPositiveButton("Set Account", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, 
             int which) { 
            dialog.dismiss(); 
           } 
          }); 
        confirm.show(); 
       } 
      } else { 
       AlertDialog.Builder confirm = new AlertDialog.Builder(
         DrawAppActivity.this); 
       confirm.setTitle(R.string.app_name); 
       confirm.setMessage("No Email Account Available!"); 
       confirm.setPositiveButton("Set Account", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int which) { 
           Intent i = new Intent(
             Settings.ACTION_SYNC_SETTINGS); 
           startActivity(i); 
           dialog.dismiss(); 
          } 
         }); 
       confirm.setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int which) { 

           dialog.dismiss(); 
          } 
         }); 
       confirm.show(); 
      } 
     } 

    }); 

    btnFacebook.setOnClickListener(new OnClickListener() { 

     public void onClick(View arg0) { 
      if (!btnFacebook.isSelected()) { 
       btnFacebook.setSelected(true); 
      } else { 
       btnFacebook.setSelected(false); 
      } 
      saveBtnFlag = 1; 
      // if (connection.isInternetOn()) { 

      if (android.os.Environment.getExternalStorageState().equals(
        android.os.Environment.MEDIA_MOUNTED)) { 
       getCurrentImage(); 
       Intent i = new Intent(DrawAppActivity.this, 
         FaceBookAuthentication.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
       startActivity(i); 
      } 

      else { 
       ShowAlertMessage.showDialog(DrawAppActivity.this, 
         R.string.app_name, R.string.Sd_card, 
         R.string.button_retry); 
      } 
     } 
    }); 
    d.show(); 

} 

public void tempImageSaving(String tmpPath, byte[] image) { 
    Random rand = new Random(); 

    tempfile = new File(Environment.getExternalStorageDirectory(), tmpPath); 
    if (!tempfile.exists()) { 
     tempfile.mkdirs(); 
    } 

    tempPath = new File(tempfile.getPath(), "DrawApp" + rand.nextInt() 
      + ".jpg"); 
    try { 
     FileOutputStream fos1 = new FileOutputStream(tempPath.getPath()); 
     fos1.write(image); 

     fos1.flush(); 
     fos1.close(); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    sendBroadcast(new Intent(
      Intent.ACTION_MEDIA_MOUNTED, 
      Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 
} 

public byte[] getCurrentImage() { 

    Bitmap b = drawingSurface.getBitmap(); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    b.compress(Bitmap.CompressFormat.PNG, 100, stream); 

    byteArray = stream.toByteArray(); 

    return byteArray; 
} 

private class emailSendingTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected void onPreExecute() { 
     progressDialog = new ProgressDialog(DrawAppActivity.this); 
     progressDialog.setTitle(R.string.app_name); 
     progressDialog.setMessage("Saving..Please Wait.."); 
     // progressDialog.setIcon(R.drawable.icon); 
     progressDialog.show(); 

    } 

    @Override 
    protected String doInBackground(String... urls) { 

     String response = ""; 
     try { 

      if (android.os.Environment.getExternalStorageState().equals(
        android.os.Environment.MEDIA_MOUNTED)) { 

       response = "Yes"; 

      } else { 
       ShowAlertMessage.showDialog(DrawAppActivity.this, 
         R.string.app_name, R.string.Sd_card, 
         R.string.button_retry); 

      } 

     } catch (Exception e) { 

      e.printStackTrace(); 
     } 

     return response; 

    } 

    @Override 
    protected void onPostExecute(String result) { 

     if (result.contains("Yes")) { 
      getCurrentImage(); 

     } 
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
       Uri.parse("file://" 
         + Environment.getExternalStorageDirectory()))); 

     progressDialog.cancel(); 
    } 
} 

private class ImageSavingTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected void onPreExecute() { 
     progressDialog = new ProgressDialog(DrawAppActivity.this); 
     progressDialog.setTitle(R.string.app_name); 
     progressDialog.setMessage("Saving..Please Wait.."); 
     // progressDialog.setIcon(R.drawable.icon); 
     progressDialog.show(); 

    } 

    @Override 
    protected String doInBackground(String... urls) { 

     String response = ""; 
     try { 

      if (android.os.Environment.getExternalStorageState().equals(
        android.os.Environment.MEDIA_MOUNTED)) { 

       response = "Yes"; 

      } else { 
       ShowAlertMessage.showDialog(DrawAppActivity.this, 
         R.string.app_name, R.string.Sd_card, 
         R.string.button_retry); 

      } 

     } catch (Exception e) { 

      e.printStackTrace(); 
     } 

     return response; 

    } 

    @Override 
    protected void onPostExecute(String result) { 

     if (result.contains("Yes")) { 
      getCurrentImage(); 

      if (CommonFunctions.createDirIfNotExists(getResources() 
        .getString(R.string.path))) 

      { 
       saveImageInSdCard(getResources().getString(R.string.path), 
         getCurrentImage()); 
      } 
     } 
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
       Uri.parse("file://" 
         + Environment.getExternalStorageDirectory()))); 

     progressDialog.cancel(); 
    } 
} 

爲Facebook應用程序使用Facebook SDK

+0

你可以請完成代碼嗎?有很多變量和其他功能丟失 –

29

是的,你可以...你只需要知道應用程序的確切包名稱:

  • Facebook的 - 「com.facebook.katana」
  • Twitter的 - 「com.twitter。機器人 「
  • Instagram的 - 」com.instagram.android「
  • Pinterest的 - 」 融爲一體。Pinterest的」

你可以創建這樣

Intent intent = context.getPackageManager().getLaunchIntentForPackage(application); 
if (intent != null) { 
    // The application exists 
    Intent shareIntent = new Intent(); 
    shareIntent.setAction(Intent.ACTION_SEND); 
    shareIntent.setPackage(application); 

    shareIntent.putExtra(android.content.Intent.EXTRA_TITLE, title); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, description); 
    // Start the specific social application 
    context.startActivity(shareIntent); 
} else { 
    // The application does not exist 
    // Open GooglePlay or use the default system picker 
} 
+0

它的工作原理,但對我來說,它缺少 shareIntent.setType(「type/plain」); –

+0

該類型可以爲每個應用程序不同。我建議動態設置它,也許通過其他方法。 –

+0

設置類型取決於你想要共享的內容。目標應用程序也應該支持這種類型。爲此,您需要在真實環境中進行測試。例如,某些應用程序直接支持圖像,而其他應用程序則不支持。 –

4

使用此

Facebook - "com.facebook.katana" 
Twitter - "com.twitter.android" 
Instagram - "com.instagram.android" 
Pinterest - "com.pinterest" 



SharingToSocialMedia("com.facebook.katana") 


public void SharingToSocialMedia(String application) { 

    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_SEND); 
    intent.setType("image/*"); 
    intent.putExtra(Intent.EXTRA_STREAM, bmpUri); 

    boolean installed = checkAppInstall(application); 
    if (installed) { 
     intent.setPackage(application); 
     startActivity(intent); 
    } else { 
     Toast.makeText(getApplicationContext(), 
       "Installed application first", Toast.LENGTH_LONG).show(); 
    } 

} 


private boolean checkAppInstall(String uri) { 
    PackageManager pm = getPackageManager(); 
    try { 
     pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); 
     return true; 
    } catch (PackageManager.NameNotFoundException e) { 
    } 

    return false; 
} 
+0

好吧,什麼是'appInstalledOrNot(應用程序)'? –

+1

@BugsHappen可能是'appInstalledOrNot(應用程序)'是http://stackoverflow.com/a/11392276/1293313 – MilapTank

+0

'appInstalledOrNot(應用程序)'它的手段來檢查慾望的應用程序沒有安裝在您的設備上或不... –

0

意圖這將讓你分享你對什麼應用程序等應用程序:

try 
      { Intent i = new Intent(Intent.ACTION_SEND); 
       i.setType("text/plain"); 
       i.putExtra(Intent.EXTRA_SUBJECT, "My application name"); 
       String sAux = "\nLet me recommend you this application\n\n"; 
       sAux = sAux + "https://play.google.com/store/apps/details?id=Orion.Soft \n\n"; 
       i.putExtra(Intent.EXTRA_TEXT, sAux); 
       startActivity(Intent.createChooser(i, "choose one")); 
1
String message = "This is testing." 
Intent shareText = new Intent(Intent.ACTION_SEND); 
shareText .setType("text/plain"); 
shareText .putExtra(Intent.EXTRA_TEXT, message); 

startActivity(Intent.createChooser(shareText , "Title of the dialog the system will open")); 
相關問題