How to share Text and Image on Facebook using Intent

Sophie picture Sophie · May 8, 2014 · Viewed 18.2k times · Source

How to share Text and Image on Facebook, I am writing a Church Application in which i want to allow user to share text and image along with URL.

I am able to share online app link but not able to share text & image, where i am missing ?

my code looks like this:

Button btnFbSharing = (Button) findViewById(R.id.fbSharing);
        btnFbSharing.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                 Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
                 shareIntent.setType("text/plain");
                 shareIntent.putExtra(android.content.Intent.EXTRA_TITLE, "Church Application");
                 shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=com.facebook.katana");
                 shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "A new world begin");

                 shareIntent.putExtra(android.content.Intent.EXTRA_STREAM, R.drawable.ic_launcher);
                 PackageManager pm = getApplicationContext().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);
                        startActivity(shareIntent);
                       }
                 }                  
            }
        });

Answer

Amardeepvijay picture Amardeepvijay · May 8, 2014

use this for sharing url in android via intent chooser... You dont share any text directly on facebook wallpost

b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String urlToShare = "www.google.com";
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                // intent.putExtra(Intent.EXTRA_SUBJECT, "Foo bar"); // NB:
                // has no effect!
                intent.putExtra(Intent.EXTRA_TEXT, urlToShare);

                // See if official Facebook app is found
                boolean facebookAppFound = false;
                List<ResolveInfo> matches = getPackageManager()
                        .queryIntentActivities(intent, 0);
                for (ResolveInfo info : matches) {
                    if (info.activityInfo.packageName.toLowerCase()
                            .startsWith("com.facebook.katana")) {
                        intent.setPackage(info.activityInfo.packageName);
                        facebookAppFound = true;
                        break;
                    }
                }

                // As fallback, launch sharer.php in a browser
                if (!facebookAppFound) {
                    String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u="
                            + urlToShare;
                    intent = new Intent(Intent.ACTION_VIEW, Uri
                            .parse(sharerUrl));
                }

                startActivity(intent);
            }
        });