Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

share intent via facebook for android jelly bean

I want to share some image and text via Facebook from android jelly bean. it work's in all devices except android jelly bean. Anybody please help me how to resolve this issue.

my code

  Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Content to share");
        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);

                System.out.println("package name"+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;

in this code i want to intent some text. But EXTRA_TEXT is not working.How to pass string or image in this EXTRA_TEXT.

like image 590
AndroidEnthusiastic Avatar asked Feb 22 '13 13:02

AndroidEnthusiastic


Video Answer


1 Answers

You shouldn't only target Facebook directly.

Instead you should just use the Share Intent. Pass your data in the extras as specified in the SDK and then have the user select which they want to share with. Don't force them to use facebook as they are NOT the only social media. Theres also twitter, 4square, and many others that the user uses. Let those apps handle your intent, don't dictate such a strict limitation as that is NOT how the intent action ACTION_SEND was meant to work.

As for your issue with the Extra, obviously EXTRA_TEXT intent will not work because its not used for images or any references like that.

Read the documentation

http://developer.android.com/reference/android/content/Intent.html#ACTION_SEND

It says:

When launching a SEND intent, you should usually wrap it in a chooser (through createChooser(Intent, CharSequence)), which will give the proper interface for the user to pick how to send your data and allow you to specify a prompt indicating what they are doing.

Input: getType() is the MIME type of the data being sent. get*Extra can have either a EXTRA_TEXT or EXTRA_STREAM field, containing the data to be sent. If using EXTRA_TEXT, the MIME type should be "text/plain"; otherwise it should be the MIME type of the data in EXTRA_STREAM. Use / if the MIME type is unknown (this will only allow senders that can handle generic data streams). If using EXTRA_TEXT, you can also optionally supply EXTRA_HTML_TEXT for clients to retrieve your text with HTML formatting.

As of JELLY_BEAN, the data being sent can be supplied through setClipData(ClipData). This allows you to use FLAG_GRANT_READ_URI_PERMISSION when sharing content: URIs and other advanced features of ClipData. If using this approach, you still must supply the same data through the EXTRA_TEXT or EXTRA_STREAM fields described below for compatibility with old applications. If you don't set a ClipData, it will be copied there for you when calling startActivity(Intent).

Optional standard extras, which may be interpreted by some recipients as appropriate, are: EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECT.

Given this information, you need to also set the mimetype for your particular data in your intent's type.

like image 152
JoxTraex Avatar answered Sep 22 '22 11:09

JoxTraex