【问题标题】:How to send an image as direct message with twitter in android?如何在 android 中使用 twitter 将图像作为直接消息发送?
【发布时间】:2014-09-16 15:11:26
【问题描述】:

我想使用 twitter4j 向关注者发送带有文本的图像。我可以发送这样的直接消息:

twitter.sendDirectMessage(twitterID, message);

现在,我不知道如何将图片作为直接消息发送。我这样做是为了发布一条推文,这很有效:

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

那么是否可以使用库 twitter4j 在 twitter 中将图像作为直接消息发送?

提前致谢。

【问题讨论】:

    标签: android image twitter sendmessage


    【解决方案1】:

    首先值得注意的是Twitter4j 做了什么。它为 Twitter 的 Java REST API 提供了良好的抽象和绑定。

    如果您查看 Twitter 的 Direct Message Endpoint,您会发现它目前提供一种在发送直接消息时“附加”图像的方法。

    这已经在Twitter Developers forums之前得到确认:

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

    【讨论】:

      【解决方案2】:

      试试这个代码:

      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

      【讨论】:

        【解决方案3】:

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

        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

        【讨论】:

        • 这个,发推文,不是直接消息。
        【解决方案4】:
        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

        【讨论】:

        • 这个,发布一条推文,就像上面一样,不是直接消息。
        • 是的,它对我有用..我也给了你一个链接,所以你也可以在那里看到
        • 我知道如何将图片发布为推文。但我不知道如何发送直接消息,你看过我的问题吗?
        猜你喜欢
        • 1970-01-01
        • 2011-05-08
        • 1970-01-01
        • 2020-07-14
        • 2012-02-09
        • 1970-01-01
        • 2017-07-04
        • 2012-06-02
        • 2020-09-10
        相关资源
        最近更新 更多