2014-05-07 111 views
1

我正在開發一個應用程序,我想分享圖像和一些文本。兩者都發送到Twitter,但我分享到Facebook圖片只分享,但不共享文本。如何分享圖像和文字到Facebook和Twitter從Android

我的代碼是:

public void open(View view) { 
    Intent sharingIntent = new Intent(Intent.ACTION_SEND); 
    Uri screenshotUri = Uri.parse("file:///sdcard/photo.jpg"); 
    sharingIntent.setType("image/*"); 
    sharingIntent.putExtra(Intent.EXTRA_TEXT, "Body text of the new status"); 
    sharingIntent.putExtra(Intent.EXTRA_TITLE, "Traffic At"); 
    sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri); 
    startActivity(Intent.createChooser(sharingIntent, "Share image using")); 
} 

回答

0
 public class Share extends Activity 
{ 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    Intent i=getIntent(); 
    String data=i.getStringExtra("data"); 
    String shareType=i.getStringExtra("type"); 

    if(shareType.equals("text")) 
    { 
     sharetext(data); 
    } 
    else if(shareType.equals("image")) 
    { 
     shareimage(data); 
    } 

} 

public void sharetext(String text) //Text to be shared 
{ 

    Intent share=new Intent(android.content.Intent.ACTION_SEND); 

    share.setType("text/plain"); 
    share.putExtra(android.content.Intent.EXTRA_SUBJECT,"TITLE"); 
    share.putExtra(android.content.Intent.EXTRA_TEXT,text); 
    startActivity(Intent.createChooser(share,"Share via")); 
    finish(); 

} 

public void shareimage(String text) //argument is image file name with extention 
{ 

    Intent shareimage=new Intent(android.content.Intent.ACTION_SEND); 


    shareimage.setType("*/*");//for all 
    shareimage.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity"); 

    shareimage.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+text)); 
    startActivity(Intent.createChooser(shareimage, "Share Image")); 
    finish(); 
} 


} 

呼叫此活動 與束含有值

StringExtra( 「數據」); //文件名或文字分享

StringExtra(「type」); //用於共享文本的「文本」,用於共享圖像的「圖像」

0

嘗試使用以下方法。

void share(String appName, String title, String text, String imagePath) { 
     try { 
      List<Intent> targetedShareIntents = new ArrayList<Intent>(); 
      Intent share = new Intent(android.content.Intent.ACTION_SEND); 
      share.setType("image/*"); 
      List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0); 
      if (!resInfo.isEmpty()) { 
       for (ResolveInfo info : resInfo) { 
        Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND); 
        targetedShare.setType("image/*"); // put here your mime type 
        if (info.activityInfo.packageName.toLowerCase().contains(appName) || info.activityInfo.name.toLowerCase().contains(appName)) { 
         targetedShare.putExtra(Intent.EXTRA_SUBJECT, title); 
         targetedShare.putExtra(Intent.EXTRA_TEXT, text); 
         targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath))); 
         targetedShare.setPackage(info.activityInfo.packageName); 
         targetedShareIntents.add(targetedShare); 
        } 
       } 
       Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share"); 
       chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{})); 
       startActivity(chooserIntent); 
      } 
     } catch (Exception e) { 
      Log.v("VM", "Exception while sending image on" + nameApp + " " + e.getMessage()); 
     } 
    } 

要通過Facebook呼叫share("facebook", title, text, imagePath),並且share("twitter", title, text, imagePath) Twitter的分享。

+0

你試過了,但圖片無法加載 – Durga

+1

我試過上面的代碼,能夠上傳圖片,但不能在Facebook上發文。你有沒有同時測試圖像和文字? –

相關問題