Mansi
Mansi

Reputation: 1949

Android: How to share image with text on facebook via intent?

I'd like to share a photo with caption pre-filled from my app via a share intent, on facebook.

Example code

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");      

intent.putExtra(Intent.EXTRA_TEXT, "eample");
intent.putExtra(Intent.EXTRA_TITLE, "example");
intent.putExtra(Intent.EXTRA_SUBJECT, "example");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);

Intent openInChooser = new Intent(intent);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);

Here is screen shot what I get

Text is not display

If a set type to image/* then a photo is uploaded without the text prefilled. If a set it to text/plain photo is not display.....

Upvotes: 50

Views: 100951

Answers (7)

DEVSHK
DEVSHK

Reputation: 863

In this Formula you can share image both Messenger and Instagram(com.instagram.android) without using any Provider in "AndroidManifest

public void shareMessenger(View v) {
    // showToast("checking");

    File dir = new File(Environment.getExternalStorageDirectory(), "MyFolder");

    File imgFile = new File(dir, "Image.png");

    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    sendIntent.setType("image/*");
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imgFile));
    sendIntent.putExtra(Intent.EXTRA_TEXT, "<---MY TEXT--->.");
    sendIntent.setPackage("com.facebook.orca");
    sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    try {
        startActivity(Intent.createChooser(sendIntent, "Share images..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(SaveAndShareActivity.this, "Please Install Facebook Messenger", Toast.LENGTH_LONG).show();
    }

}

**Add this two line in onCreate Method **

 StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

Upvotes: 1

Eduardo Oliveira
Eduardo Oliveira

Reputation: 646

The newest Facebook versions doesn't allow you to share text using intents. You have to use the Facebook SDK to do it - to make that simple, use the Facebook SDK + Android Simple Facebook (https://github.com/sromku/android-simple-facebook). Using the library, your code would be like this (extracted from the Simple Facebook site):

Publish feed

Set OnPublishListener and call for:

  • publish(Feed, OnPublishListener) without dialog.
  • publish(Feed, true, OnPublishListener) with dialog.

Basic properties

  • message - The message of the user
  • name - The name of the link attachment
  • caption - The caption of the link (appears beneath the link name)
  • description - The description of the link (appears beneath the link caption)
  • picture - The URL of a picture attached to this post. The picture must be at least 200px by 200px
  • link - The link attached to this post

Initialize callback listener:

OnPublishListener onPublishListener = new OnPublishListener() {
    @Override
        public void onComplete(String postId) {
            Log.i(TAG, "Published successfully. The new post id = " + postId);
        }

     /* 
      * You can override other methods here: 
      * onThinking(), onFail(String reason), onException(Throwable throwable)
      */
};

Build feed:

Feed feed = new Feed.Builder()
    .setMessage("Clone it out...")
    .setName("Simple Facebook for Android")
    .setCaption("Code less, do the same.")
    .setDescription("The Simple Facebook library project makes the life much easier by coding less code for being able to login, publish feeds and open graph stories, invite friends and more.")
    .setPicture("https://raw.github.com/sromku/android-simple-facebook/master/Refs/android_facebook_sdk_logo.png")
    .setLink("https://github.com/sromku/android-simple-facebook")
    .build();

Publish feed without dialog:

mSimpleFacebook.publish(feed, onPublishListener);

Publish feed with dialog:

mSimpleFacebook.publish(feed, true, onPublishListener);


Update on 14 December 2015


according to New Facebook SDK.

facebook-android-sdk:4.6.0

It's very Simple.
1. create Provider in Android.manifest.xml

<provider
            android:authorities="com.facebook.app.FacebookContentProvider{APP_ID}"
            android:name="com.facebook.FacebookContentProvider"
            android:exported="true" />

2. Create Your Share Intent with Data.

ShareHashtag shareHashTag = new ShareHashtag.Builder().setHashtag("#YOUR_HASHTAG").build();
ShareLinkContent shareLinkContent = new ShareLinkContent.Builder()
                .setShareHashtag(shareHashTag)
                .setQuote("Your Description")
                .setContentUrl(Uri.parse("image or logo [if playstore or app store url then no need of this image url]"))
                .build();


3. Show The Share Dialog

ShareDialog.show(ShowNavigationActivity.this,shareLinkContent);


That's It.

Upvotes: 47

Baker
Baker

Reputation: 28060

As of 2017, facebook doesn't allow sharing of an image + text together, directly from your app.

Workaround

Facebook will though, scrape a URL for title and image data and will use that in a share post.

As a workaround you could create a single page application* that dynamically loads the text/image you want to share (specified in the URL) and you can facebook-share that URL.

Notes:

  • Ensure your single page application produces a static page which has its title, open graph meta tags, and images set prior to facebook's page scrape. If these web page tags are changed dynamically through Javascript, facebook will not be able to scrape those values and use them in its share post.
  • Use open graph meta property tags og:image:height and og:image:width to allow facebook to create an image preview within its share post

Steps

0) add the latest facebook-sdk library to your build.gradle file

compile group: 'com.facebook.android', name: 'facebook-android-sdk', version: '4.25.0'

1) In your AndroidManifest.xml, add a meta-data tag within your <application> section:

<application android:label="@string/app_name" ...>
...
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
...
</application>

Add a facebook_app_id string (with your APP ID) to your strings.xml file:

<string name="facebook_app_id">12341234</string>

YOURFBAPPID is your Facebook App ID number found at https://developers.facebook.com/apps/

2) also add a <provider> tag outside of your <application> tag in AndroidManifest.xml

<provider android:authorities="com.facebook.app.FacebookContentProviderYOURFBAPPID"
          android:name="com.facebook.FacebookContentProvider"
          android:exported="true"/>

3) Create a ShareLinkContent object using their builder:

ShareLinkContent fbShare = new ShareLinkContent.Builder()
            .setContentUrl(Uri.parse("http://yourdomain.com/your-title-here/someimagefilename"))
            .build();

4) Share it from your fragment (or activity, etc.):

ShareDialog.show(getActivity(), fbShare);

Facebook Docs

https://developers.facebook.com/docs/android/getting-started

Upvotes: 4

Deepshikha Puri
Deepshikha Puri

Reputation: 2094

Without using Facebook sdk we can't share the image and text simultaneously on facebook. To solve this problem I had create a bitmap of image and text, Share that bitmap on facebook and it's working perfectly.

You can download the source code from here (Share image and text on facebook using intent in android)

Here is code:

MainActivity.java

package com.shareimage;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {
EditText et_text;
ImageView iv_image;
TextView tv_share,tv_text;
RelativeLayout rl_main;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    init();

}

private void init(){
    et_text = (EditText)findViewById(R.id.et_text);
    iv_image = (ImageView)findViewById(R.id.iv_image);
    tv_share = (TextView)findViewById(R.id.tv_share);
    rl_main = (RelativeLayout)findViewById(R.id.rl_main);
    tv_text= (TextView) findViewById(R.id.tv_text);

    File dir = new File("/sdcard/Testing/");
    try {
        if (dir.mkdir()) {
            System.out.println("Directory created");
        } else {
            System.out.println("Directory is not created");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    tv_share.setOnClickListener(this);

    et_text.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            tv_text.setText(et_text.getText().toString());

        }
    });


}




@Override
public void onClick(View v) {

    switch (v.getId()){
        case R.id.tv_share:
            Bitmap bitmap1 = loadBitmapFromView(rl_main, rl_main.getWidth(), rl_main.getHeight());
            saveBitmap(bitmap1);
            String str_screenshot = "/sdcard/Testing/"+"testing" + ".jpg";

            fn_share(str_screenshot);
            break;
    }

}

public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File("/sdcard/Testing/"+"testing" + ".jpg");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();

        Log.e("ImageSave", "Saveimage");
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

public static Bitmap loadBitmapFromView(View v, int width, int height) {
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.draw(c);

    return b;
}

public void fn_share(String path) {

    File file = new File("/mnt/" + path);

    Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
    Uri uri = Uri.fromFile(file);
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(intent, "Share Image"));


}

}

Upvotes: -1

Vaiden
Vaiden

Reputation: 16132

FB no longer allows you to prefill the sharing message.

In order to circumvent this, you will need to use an SDK to publish via a Graph request. For this you will need the publish_actions permission. Since last month you need to submit your app to a review process to gain access to publish_actions. Which you would fail if your app prefills the sharing texts. Trust me - I've had the Chutzppah to try.

So it looks like we would have to comply.

B.t.w. in iOS you can still prefill the texts using the FB sdk. Who knows for how long.

Upvotes: 2

Karthik P B
Karthik P B

Reputation: 237

Add these lines to your following code

shareCaptionIntent.putExtra(Intent.EXTRA_TITLE, "my awesome caption in the EXTRA_TITLE field");

Upvotes: -1

Vaishali Sutariya
Vaishali Sutariya

Reputation: 5121

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);

   shareIntent.setType("image/*");

   shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));

   shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); // put your image URI

   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;
        }
      }

Upvotes: -3

Related Questions