簡體   English   中英

如何從Android將圖像和文本共享到Facebook和Twitter

[英]How to Share Image And Text to Facebook and Twitter From Android

我正在開發一個應用程序,因為我想共享圖像和一些文本。 兩者都發送到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"));
}
      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( “輸入”); //“文本”用於共享文本,“圖像”用於共享圖像

嘗試使用以下方法。

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調用share("facebook", title, text, imagePath)share("twitter", title, text, imagePath)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM