Using update_with_media with Twitter4J

Twitter recently introduced a new API called “update_with_media”. It allows users to post pictures and other media directly to Twitter, rather than using a third-party platform like

The API is documented here:

Twitter4J is an excellent library for integrating Twitter support into Java applications, including those running on Android. Twitter4J is available here:

I was recently using Twitter4J 2.2.5, and found myself wanting to use the new Twitter API to post some pictures. There didn’t seem to be an available method in the library, though, that made use of the newly published Twitter endpoint, and a quick search of the codebase for “update_with_media” turned up nothing, so I decided to modify Twitter4J to support “update_with_media”. The changes are actually relatively painless, and are documented below:

1. Find the “StatusMethods” class in the “twitter4j.api” package, and add the following interface”:

Status updateStatusWithFileMedia(String status,File media) throws TwitterException;

We’re going to be uploading the media (a JPG image, in this case) directly from its associated File object on the filesystem.

2. Open up the “TwitterImpl” object in the “twitter4j” package, and implement interface as follows:

    public Status updateStatusWithFileMedia(String status, File media) 
        throws TwitterException {
        // check authorization
    	// construct the custom request
    	String url = "";
    	HttpParameter p1 = new HttpParameter("status",status);
    	HttpParameter p2 = new HttpParameter("media[]",media);
    	HttpParameter[] arr = { p1, p2 };
    	// make call to factory method
    	return factory.createStatus(post(url,arr));

and that’s it!

We can now upload media files (i.e., JPG files) directly to Twitter using the following sample code:


import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class Driver {
	public static void main(String[] args) {
                // configure your twitter access tokens
		ConfigurationBuilder cb = new ConfigurationBuilder();
                // grab a factory instance and post to twitter!	
		TwitterFactory tf = new TwitterFactory(;
		Twitter twitter = tf.getInstance();
		try {
			twitter.updateStatusWithFileMedia("Pictures of #saturn",new File("Saturn1.jpg"));
		} catch ( Exception ex ) {


2 thoughts on “Using update_with_media with Twitter4J

    • The best way to “change” a .class file is simply to recompile the corresponding .java file using javac.

      Bear in mind also that it’s been a while since I posted this.

      The more recent versions of Twitter4J I think already incorporate this change (or so I’ve been told, I haven’t followed up to verify).

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 )

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