4

FacebookやTwitterを介してテキスト(およびその後の写真も)を共有できるAndroidアプリを開発しています。facebook/twitter 共有ウィンドウを開くコードを見つけましたが、共有する必要があるテキストはアプリの EditText にあるため、私の質問は、EditText から共有されるテキストにテキストを挿入する方法です (申し訳ありません)英語が下手で、さらに説明が必要な場合、または何も理解できなかった場合は、私もそれをよりよく説明しようとします)。これは、両方の共有方法の私のコードです。

フェイスブック:

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){
}
}

これをフェイスブックに使用してください:-

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

gmail、facebook、twitter に画像を添付するには、以下のコードを使用します。

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

// share(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());
    }
  }
于 2013-05-28T09:43:56.310 に答える