How to share Text and Image on Facebook using Intent

▼魔方 西西 提交于 2019-12-03 16:09:12
Iain Smith

Hey I did a lot of research into this as I asked the same question

Basically it is not possible from the facebook app using the package name "com.facebook.katana" as it ignores the extra text when the image is there see this for actual bug but can have links when the image is not there. Very annoying I know.

After a lot of looking about I created my own activity using the facebook sdk 3.14.1 which allows images and text here is the github link to the demo project give it a go and let know if it helps you out.

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);
            }
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!