如何在android中发送图像作为直接消息与twitter?

时间:2014-09-16 15:11:26

标签: android image twitter sendmessage

我想使用twitter4j向关注者发送包含文字的图片。我可以发送这样的直接消息:

twitter.sendDirectMessage(twitterID, message);

现在,我无法弄清楚如何将图像作为直接消息发送。我这样做是为了发布一条推文,它有效:

StatusUpdate status = new StatusUpdate(message);
            status.setMedia(pathOfTheFileToSend);
            twitter.updateStatus(status);

那么可以使用图书馆twitter4j在Twitter上发送图像作为直接消息吗?

提前致谢。

4 个答案:

答案 0 :(得分:2)

首先,值得注意Twitter4j的作用。它为Java中的Twitter REST API提供了良好的抽象和绑定。

如果你看一下Twitter Direct Message Endpoint,你会发现目前提供了一种方式来附加"发送直接消息时的图像。

此前已在Twitter Developers forums确认:

  

我们尚未宣布提供媒体上传端点的计划   直接消息。

答案 1 :(得分:0)

试用此代码:

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();
    }
}

参考:GitHub Project AndroidTwitPic

答案 2 :(得分:-1)

使用以下代码发送带有文字的图像

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);

有关详细说明,请查看此tutorial

答案 3 :(得分:-2)

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;
    }

}

或者你可以refer this