通过Android应用程序在Twitter和Facebook上分享

时间:2012-12-17 15:56:17

标签: android facebook twitter sharing

我正在开发一个adroid应用程序,我可以通过Facebook和Twitter分享文本(以及后来的图片)。我找到了一些打开facebook / twitter共享窗口的代码,但是需要共享的文本是在应用程序的EditText中,所以我的问题是如何将我的文本从EditText插入到将要共享的文本中(抱歉)对于糟糕的英语,如果你需要进一步的解释或你什么都不懂,那么我会尝试更好地解释它)。这是我的共享方法的代码:

Facebook的:

Intent shareIntent = new Intent(
                    android.content.Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                    (String) v.getTag(R.string.app_name));
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                    (String) v.getTag(R.drawable.ic_launcher));
            PackageManager pm = v.getContext().getPackageManager();
            List<ResolveInfo> activityList = pm.queryIntentActivities(
                    shareIntent, 0);
            for (final ResolveInfo app : activityList) {
                if ((app.activityInfo.name).contains("facebook")) {
                    final ActivityInfo activity = app.activityInfo;
                    final ComponentName name = new ComponentName(
                            activity.applicationInfo.packageName,
                            activity.name);
                    shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    shareIntent.setComponent(name);
                    v.getContext().startActivity(shareIntent);
                    break;
                }
            }

Twitter的:

Intent shareIntent = new Intent(
                    android.content.Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                    (String) v.getTag(R.string.app_name));
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                    (String) v.getTag(R.drawable.ic_launcher));
            PackageManager pm = v.getContext().getPackageManager();
            List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
            for (final ResolveInfo app : activityList) {
                if ("com.twitter.android.PostActivity"
                        .equals(app.activityInfo.name)) {
                    final ActivityInfo activity = app.activityInfo;
                    final ComponentName name = new ComponentName(
                            activity.applicationInfo.packageName,
                            activity.name);
                    shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    shareIntent.setComponent(name);
                    v.getContext().startActivity(shareIntent);
                    break;
                }
            }

2 个答案:

答案 0 :(得分:3)

使用此方法与文本共享照片。调用此方法并传递参数nameofapp和imagepath。应用程序名称意味着您要分享的内容,如gmail,facebook,twitter。

private void share(String nameApp, String imagePath,String text) {
try
{
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("image/jpeg");
    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/jpeg"); // put here your mime type
                if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
                    targetedShare.putExtra(Intent.EXTRA_SUBJECT, text);
                    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){
}
}

将此用于Facebook: -

try {
File filePath = "/mnt/sdcard/vmphoto.jpg"; //This is imagefile path in your change it acc. to your requirement.
share("facebook",filePath.toString(),"Hello"); <<<<<Hello is text. send acc. to you req.

}
catch(Exception e) {
       //exception occur might your app like gmail , facebook etc not installed or not working correctly.
}

对于推特

try {
File filePath = "/mnt/sdcard/vmphoto.jpg"; //This is imagefile path in your change it acc. to your requirement.
share("twitter",filePath.toString(),"Hello"); <<<<<Hello is a text send acc. to you req.

}
catch(Exception e) {
       //exception occur might your app like gmail , facebook etc not installed or not working correctly.
}

答案 1 :(得分:2)

用于在gmail,facebook,twitter上附加图像,文字使用以下代码。

File filePath = context.getFileStreamPath("vmphoto.jpg");
share("gmail",filePath.toString());
share("facebook",filePath.toString());
share("twitter",filePath.toString());

//共享代码(String nameApp,String imagePath)函数

  void share(String nameApp, String imagePath) {
 try
{
  List<Intent> targetedShareIntents = new ArrayList<Intent>();
  Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("image/jpeg");
  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/jpeg"); // put here your mime type
        if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
            targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Sample Photo");
         targetedShare.putExtra(Intent.EXTRA_TEXT,"This photo is created by App Name");
            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());
    }
  }