Sharing Text & Image In Twitter – Android Example

Out of 100 application 70 to 80 apps have sharing option either it could be an Android , Iphone or any other platform user can see the sharing Icon which having all social media like FaceBook, Google Plus (G+),Twitter, Mail,etc.

So now a day’s social media is a one of the major part of the application as well as user.

Well here I will share you one of them and that is Twitter Integration in android. So let’s start with coding.

In this post you will learn from starting if you had never implement Twitter in android. You will learn how to share your text message as well as picture also.

In first stage we will learn what should be minimum requirement to integrate Twitter.


Updates :

Dear All i know that my last code was not working and that is due to  Twitter4J lib and i have received so many comments to fix it so finally i have build same code with few changes as below.

1. Replace latest jar :

For latest .jar file you can download from , once you extract the folder you have to replace twitter4j-core-4.0.2.jar & twitter4j-media-support-4.0.2.jar inside the /lib.

2. Fix the Bug;

In previous code i was updating ui (Toast Message) from background thread so that will cause the issue and crashing the application. Now i have update with runOnUiThread so that will not throw the exception.

3. Emulator Testing:

Add SD card size if you are testing in emulator otherwise that will throw Permission error.

And you can get latest code from my Git Page.

Follow the below steps:

  • Open the
  • My Application > Create New Application
  • Fill up some basic >>[ App Name, Description, Web Site, Call back URL ] Everything is mandatory so don’t blank anything you can read more details on same page.
  • Click on Checkbox to agree terms and condition
  • Write captcha and submit you will be get message to done successfully along with that you will be redirect to your KEY Screen and here is your data which will be use to implement in android.
  • You will get Consumer Key & Consumer Secrete Key
  • Now select the setting
  • > check on Read,Write and Access direct Message
  • > also check “Allow this application to be use to sign in with twitter”
Create New Application - Twitter
Create New Application – Twitter

Change your Two Keys & Run Sample

public final String consumer_key = "Replace your KEY";
public final String secret_key = "Replace your KEY";

Twitte to Twitter

public void onClickTwitt() {
	if (isNetworkAvailable()) {
	    Twitt_Sharing twitt = new Twitt_Sharing(MainActivity.this,
		    consumer_key, secret_key);
	    string_img_url = "";
	    string_msg = "";
	    // here we have web url image so we have to make it as file to
	    // upload
	    // Now share both message & image to sharing activity
	    twitt.shareToTwitter(string_msg, casted_image);

	} else {
	    showToast("No Network Connection Available !!!");

How to convert String Image URL to File or Image ?
// this function will make your image to file

public File String_to_File(String img_url) {

	try {
	    File rootSdDirectory = Environment.getExternalStorageDirectory();

	    casted_image = new File(rootSdDirectory, "attachment.jpg");
	    if (casted_image.exists()) {

	    FileOutputStream fos = new FileOutputStream(casted_image);

	    URL url = new URL(img_url);
	    HttpURLConnection connection = (HttpURLConnection) url
	    InputStream in = connection.getInputStream();

	    byte[] buffer = new byte[1024];
	    int size = 0;
	    while ((size = > 0) {
		fos.write(buffer, 0, size);
	    return casted_image;

	} catch (Exception e) {

	    // e.printStackTrace();

	return casted_image;

Now up to here i have describe how to config Twitter and how to share image and text in Twitter.Download full code and learn Twitter other files also. How they manage the User REQUEST and return RESPONSE according to same.

I haven’t write a code for Login session so now that is your task how maintain login session and how to Log out.

Hope you like it my post and you will learn grate things form this post.

Twitter Sharing Twitter Login Dialog


57 thoughts on “Sharing Text & Image In Twitter – Android Example

  1. It’s good example but it’s not posting for new version 4.2 devices in android.Please Update the code for new version 4.2. as soon as possible.


  2. Hello Chintan I think so u have posted nice sample but using ur code and ur sample consumer key i m getting error as (Posting failed!!!) so can u plz elaborate the following????


  3. I am testing on Samsung Galaxy s4.
    I am getting error as.
    “Pic Upload errorError creating status.”
    Can you please check, why it is not send tweets in 4.2.


  4. Not Posting data. “Posting Failed” appeares. Logcat below:

    01-07 06:49:54.564: W/System.err(1255): java.lang.RuntimeException: Can’t create handler inside thread that has not called Looper.prepare()
    01-07 06:49:54.564: W/System.err(1255): at android.os.Handler.(
    01-07 06:49:54.594: W/System.err(1255): at android.os.Handler.(
    01-07 06:49:54.604: W/System.err(1255): at android.widget.Toast$TN.(
    01-07 06:49:54.604: W/System.err(1255): at android.widget.Toast.(
    01-07 06:49:54.604: W/System.err(1255): at android.widget.Toast.makeText(
    01-07 06:49:54.604: W/System.err(1255): at com.twittershare.Twitter_code.Twitt_Sharing.Share_Pic_Text_Titter(
    01-07 06:49:54.614: W/System.err(1255): at com.twittershare.Twitter_code.Twitt_Sharing$PostTwittTask.doInBackground(
    01-07 06:49:54.614: W/System.err(1255): at com.twittershare.Twitter_code.Twitt_Sharing$PostTwittTask.doInBackground(
    01-07 06:49:54.614: W/System.err(1255): at android.os.AsyncTask$
    01-07 06:49:54.614: W/System.err(1255): at
    01-07 06:49:54.655: W/System.err(1255): at android.os.AsyncTask$SerialExecutor$
    01-07 06:49:54.655: W/System.err(1255): at java.util.concurrent.ThreadPoolExecutor.runWorker(
    01-07 06:49:54.655: W/System.err(1255): at java.util.concurrent.ThreadPoolExecutor$
    01-07 06:49:54.734: W/System.err(1255): at


    1. Yeah even i have face that issue while i was trying to run that demo and that is issue of Twitter 4j Lib i have update some code in my older project i will update soon.


  5. I am stuck with callback. It will not redirect to myapplication after authorization. will you please explain what to write in place of callback. its urgent.


  6. WIth ur callback i am getting a toast message login fail. I don’t understand what is the problem. will you please help


    1. Yeah even i have face that issue while i was trying to run that demo and that is issue of Twitter 4j Lib i have update some code in my older project i will update soon.


  7. Here i want to tweet from android app without login .
    i have

    is it possible ,if possible could you suggest me.


  8. I have many problem with this sample code. After all, I am really thank you for your source code, and I have advise for all people use this source, you should find latest libs to run it.


  9. Hi Chintan
    when i am shearing a image it showing login failed.when i am giving correct twitter user name and password it is not working me.could help the where i change the code


  10. Hi!
    “Yeah even i have face that issue while i was trying to run that demo and that is issue of Twitter 4j Lib i have update some code in my older project i will update soon”
    You solve problems related to Twitter 4j lib?
    Thanks you :).


  11. HI chintan thanks for the cdoe . I want to open the sharing window before posting so that user can change the text how can i do this please reply


  12. working well but with replace the latest twitter jar file twitter4j-core-3.0.4.jar intstead of twitter4j-core-3.0.3.jar


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s