4

我正在开发一个安卓应用程序,我可以通过 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;
                }
            }

推特:

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;
                }
            }
4

2 回答 2

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.
}
于 2012-12-17T16:09:31.870 回答
2

For attaching image on gmail,facebook,twitter with text use below code.

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

// code of share(String nameApp,String imagePath) function

  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());
    }
  }
于 2013-05-28T09:43:56.310 回答