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   http://twitter4j.org/en/ , 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. https://github.com/khetiyachintan/Android-Twitter-Example

Follow the below steps:

  • Open the https://dev.twitter.com/
  • 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 = "http://3.bp.blogspot.com/_Y8u09A7q7DU/S-o0pf4EqwI/AAAAAAAAFHI/PdRKv8iaq70/s1600/id-do-anything-logo.jpg";
	    string_msg = "https://chintankhetiya.wordpress.com/";
	    // here we have web url image so we have to make it as file to
	    // upload
	    String_to_File(string_img_url);
	    // 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()) {
		casted_image.delete();
	    }
	    casted_image.createNewFile();

	    FileOutputStream fos = new FileOutputStream(casted_image);

	    URL url = new URL(img_url);
	    HttpURLConnection connection = (HttpURLConnection) url
		    .openConnection();
	    connection.setRequestMethod("GET");
	    connection.setDoOutput(true);
	    connection.connect();
	    InputStream in = connection.getInputStream();

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

	} catch (Exception e) {

	    System.out.print(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

GitHub-download
GitHub-download

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.

    Like

  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????

    Like

  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.

    Like

  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.(Handler.java:197)
    01-07 06:49:54.594: W/System.err(1255): at android.os.Handler.(Handler.java:111)
    01-07 06:49:54.604: W/System.err(1255): at android.widget.Toast$TN.(Toast.java:324)
    01-07 06:49:54.604: W/System.err(1255): at android.widget.Toast.(Toast.java:91)
    01-07 06:49:54.604: W/System.err(1255): at android.widget.Toast.makeText(Toast.java:238)
    01-07 06:49:54.604: W/System.err(1255): at com.twittershare.Twitter_code.Twitt_Sharing.Share_Pic_Text_Titter(Twitt_Sharing.java:134)
    01-07 06:49:54.614: W/System.err(1255): at com.twittershare.Twitter_code.Twitt_Sharing$PostTwittTask.doInBackground(Twitt_Sharing.java:91)
    01-07 06:49:54.614: W/System.err(1255): at com.twittershare.Twitter_code.Twitt_Sharing$PostTwittTask.doInBackground(Twitt_Sharing.java:1)
    01-07 06:49:54.614: W/System.err(1255): at android.os.AsyncTask$2.call(AsyncTask.java:287)
    01-07 06:49:54.614: W/System.err(1255): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    01-07 06:49:54.655: W/System.err(1255): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    01-07 06:49:54.655: W/System.err(1255): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    01-07 06:49:54.655: W/System.err(1255): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    01-07 06:49:54.734: W/System.err(1255): at java.lang.Thread.run(Thread.java:856)

    Like

    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.

      Like

  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.

    Like

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

    Like

    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.

      Like

  7. Here i want to tweet from android app without login .
    i have
    public static final String CONSUMER_KEY = “XXXXXXXXXXXXXXXXXXXXXXXXXXX”;
    public static final String CONSUMER_SECRET =”XXXXXXXXXXXXXXXXXXXXXXXXXXXX”;

    public static final String OATH_TOKEN=”4XXXXXXXXXXXXXXXXXXXXXXXXXXX”;
    public static final String OATH_TOKEN_SECRET=”XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;
    is it possible ,if possible could you suggest me.

    Like

  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.

    Like

  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

    Like

  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 :).

    Like

  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

    Like

  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

    Like

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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