如何从Android分享图像和文本到Facebook和Twitter

时间:2014-05-07 07:50:09

标签: android facebook twitter

我正在开发一个应用程序,因为我想分享Image和一些文本。两者都发送到Twitter,但我分享到Facebook图像只共享但不共享文本。

我的代码是:

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"));
}

2 个答案:

答案 0 :(得分: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("数据&#34); //要共享的文件名或文本

StringExtra("类型&#34); //"文字"用于分享文字,"图像"用于分享图像

答案 1 :(得分: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。