Home » Android » eclipse – share intent via facebook for android jelly bean

eclipse – share intent via facebook for android jelly bean

Posted by: admin June 16, 2020 Leave a comment

Questions:

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.

How to&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.

Answer:

Try out the below code. I have used this code to share the Text & Image from one of my application and its working great for me.

I hope it will work for you also.

  File pngDir = new   File(Environment.getExternalStorageDirectory(),"Android/data/Textures");
   if (!pngDir.exists()) {
pngDir.mkdirs();
}
File pngfile = new File(pngDir,"texture1.png");
Uri pngUri = Uri.fromFile(pngfile);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                "Text to Share");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
        "Context to share");
shareIntent.putExtra(android.content.Intent.EXTRA_STREAM,
                pngUri); //Share the image on Facebook
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);
        break;
            }
        }

In this i have used the image stored at particular location in the device. You can change it according to your image location.

Answer:

Please refer the FaceBook Page for more details

It looks like we can not pass a text to share in Facebook App.

Facebook Share Intent Details