How to send an image as a direct message using twitter in android?

I want to send an image with text to a subscriber using twitter4j. I can send a direct message like this:

twitter.sendDirectMessage(twitterID, message);


Now I cannot figure out how to send the image as a direct message. I did this to post a tweet, which works:

StatusUpdate status = new StatusUpdate(message);


So, is it possible to send an image as a direct message on twitter using the twitter4j library?

Thanks in advance.


source to share

4 answers

First, it's worth noting what it does Twitter4j

. It provides nice abstraction and bindings to the Twitter REST API in Java.

If you look at Twitter Direct Message Endpoint , you will see that it does not currently provide a way to "attach" an image when sending a direct message.

This is confirmed on the Twitter developer forum :

We do not yet have announced plans to provide a multimedia download endpoint for direct messages.



Try this code:

private class ImageSender extends AsyncTask<URL, Integer, Long> {
    private String url;

    protected void onPreExecute() {
        //set progress dialog

    protected Long doInBackground(URL... urls) {            
        long result = 0;

        TwitterSession twitterSession   = new TwitterSession(YourActivity.this);            
        AccessToken accessToken         = twitterSession.getAccessToken();

        Configuration conf = new ConfigurationBuilder()                 

        OAuthAuthorization auth = new OAuthAuthorization (conf, conf.getOAuthConsumerKey (), conf.getOAuthConsumerSecret (),
                new AccessToken (conf.getOAuthAccessToken (), conf.getOAuthAccessTokenSecret ()));

        ImageUpload upload = ImageUpload.getTwitpicUploader (twitpic_api_key, auth); // get the key from

        try {
            url = upload.upload(new File(mPath));
            result = 1;     
        } catch (Exception e) {                     
        return result;

    protected void onProgressUpdate(Integer... progress) {            

    protected void onPostExecute(Long result) {         
        String text = (result == 1) ? "Image sent successfully.\n Twitpic url is: " + url : "Failed to send image";         
        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();


Refernce: GitHub AndroidTwitPic Project



Use the following code to send an image with text

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();





Configuration configuration =;

Twitter twitter = new TwitterFactory(configuration).getInstance();

StatusUpdate status = new StatusUpdate(message);

status.setMedia(file); // set the image to be uploaded here.



You can learn more about this in the tutorial .



public void tweetPicture(File file, String message) throws Exception  {


    StatusUpdate status = new StatusUpdate(message);

    catch(TwitterException e){
        Log.d("TAG", "Pic Uploading error" + e.getErrorMessage());
        throw e;



OR you can refer to this



All Articles