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);
            status.setMedia(pathOfTheFileToSend);
            twitter.updateStatus(status);

      

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

Thanks in advance.

+3


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.

+2


source


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()                 
        .setOAuthConsumerKey(twitter_consumer_key) 
        .setOAuthConsumerSecret(twitter_secret_key) 
        .setOAuthAccessToken(accessToken.getToken()) 
        .setOAuthAccessTokenSecret(accessToken.getTokenSecret()) 
        .build(); 

        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 http://dev.twitpic.com/

        try {
            url = upload.upload(new File(mPath));
            result = 1;     
        } catch (Exception e) {                     
            e.printStackTrace();
        }           
        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

0


source


Use the following code to send an image with text

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

   configurationBuilder.setOAuthConsumerKey(context.getResources().getString(R.string.twitter_consumer_key));

     configurationBuilder.setOAuthConsumerSecret(context.getResources().getString(R.string.twitter_consumer_secret));

configurationBuilder.setOAuthAccessToken(LoginActivity.getAccessToken((context)));

configurationBuilder.setOAuthAccessTokenSecret(LoginActivity.getAccessTokenSecret(context));

Configuration configuration = configurationBuilder.build();

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

StatusUpdate status = new StatusUpdate(message);

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

twitter.updateStatus(status);

      

You can learn more about this in the tutorial .

-1


source


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

  try{

    StatusUpdate status = new StatusUpdate(message);
    status.setMedia(file);
    mTwitter.updateStatus(status);}

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

}

      

OR you can refer to this

-2


source







All Articles