Android: How to share image with text on facebook via intent?
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 username
- The name of the link attachmentcaption
- 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 200pxlink
- 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.
Share Image and Text to Facebook on android
Was able to do this my self with the current Facebook SDK (3.14.1 at the time) with no login and I made it into a share intent for adding to the chooser list.
I have a demo project at https://github.com/b099l3/FacebookImageShareIntent only dependency is the facebook sdk and it is contained in one activity.
How to share Text and Image on Facebook using Intent
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.
Share image in Facebook programmatically from Android app via Intent
Could the reason be some security restrictions, e.g. The FB app does not have rights to access the image in the application folder even though it is invoked from an intent?
Correct. That image is on internal storage for your app, which is private to your app.
If so, what would be a proper location for an image shared between the apps?
You can stick with internal storage, though you will need to use a FileProvider
, perhaps with my LegacyCompatCursorWrapper
, to serve the file. This sample app demonstrates this, albeit with a PDF rather than an image.
Or, put the file on external storage.
Shall I use something like this: how to share image to facebook via intent
You could, though that would seem to be overkill, compared to using FileProvider
.
Share text and image with android intent
If you want to share the Image and text on Facebook without using Facebook SDK then you have to create the bitmap of your image plus text and then you can share that bitmap on facebook.
Download the source code from here (Share image and text on facebook using intent in android)
activity_main.xml
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/et_text"
android:layout_width="match_parent"
android:textSize="15dp"
android:layout_height="45dp"
android:layout_marginTop="10dp"
android:background="@drawable/edittext_drawable"
android:hint="Enter your text"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:paddingRight="10dp"
android:inputType="text"
android:imeOptions="actionDone"
android:paddingLeft="10dp"
android:singleLine="true"
android:textColorHint="#979797" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl_main"
android:background="#ffffff"
android:layout_below="@+id/et_text"
android:layout_above="@+id/tv_share">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="@drawable/index"
android:scaleType="fitXY"
android:id="@+id/iv_image"
android:layout_marginTop="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:id="@+id/tv_text"
android:layout_below="@+id/iv_image"
android:layout_margin="10dp"
android:textColor="#000000"
android:maxLines="5"
/>
</RelativeLayout>
<TextView
android:id="@+id/tv_share"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#F38D0A"
android:gravity="center"
android:padding="10dp"
android:layout_margin="10dp"
android:text="Share"
android:textColor="#ffffff"
android:textSize="15dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
MainActivity.java
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"));
}
}
Thanks!
How to Share Image+Text using Share Intent in android
Try out the below code:
String fileName = "image-3116.jpg";//Name of an image
String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
String myDir = externalStorageDirectory + "/saved_images/"; // the file will be in saved_images
Uri uri = Uri.parse("file:///" + myDir + fileName);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test Mail");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Launcher");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Deal"));
Share image and text through Whatsapp or Facebook
Please try the below code and hopefully it will work.
Uri imgUri = Uri.parse(pictureFile.getAbsolutePath());
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
whatsappIntent.setType("image/jpeg");
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
Related Topics
How to Get the Current Orientation (Activityinfo.Screen_Orientation_*) of an Android Device
Android Slidingdrawer from Top
Run Code Only Once After an Application Is Installed on Android Device
How to Keep an Alertdialog Open After Button Onclick Is Fired
Icon in Menu Not Showing in Android
An Android App Remembers Its Data After Uninstall and Reinstall
How to Format Datetime in Flutter
Webview Showing Err_Cleartext_Not_Permitted Although Site Is Https
Android: Crop an Image After Taking It with Camera with a Fixed Aspect Ratio
Confusion on Yuv Nv21 Conversion to Rgb
Android Webview Inside Scrollview Scrolls Only Scrollview
How to Set the Language in Speech Recognition on Android
Popupwindow $Badtokenexception: Unable to Add Window -- Token Null Is Not Valid
File Res/Drawable/Abc_Ic_Ab_Back_Material.Xml from Drawable Resource Id #0X7F020016
How to Prevent Going Back to the Previous Activity
Android View Not Attached to Window Manager
Android N Requires the Ide to Be Running with Java 1.8 or Later