android - 我们可以在 Android 中使用 twitter API 在 twitter 上发布图片吗?

标签 android twitter

我已经成功集成了 Twitter API,我可以从我的设备发布文本,但我想知道两件事

  1. 是否可以在 Android 中使用 API 在 twitter 上发布图片?

  2. 在 twitter 中我们使用了 OAuth.OAUTH_TOKENOAuth.OAUTH_TOKEN_SECRET token 。我在下面的代码中将 token 值传递给第二个参数可以吗?或者我必须将其留空?

    String token = prefs.getString(OAuth.OAUTH_TOKEN, OAuth_token_key);
    String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, OAuth_token_secret);
    

我搜索过是否可以在 Android 中使用 twitter API 在 twitter 上发布图片,但我没有找到任何我知道是否可能的链接。

我遇到了一个类似的 iPhone 帖子图片问题there is a answer also .我不了解 iPhone,所以我不知道它是否是正确答案。这是 iPhone 的帖子图片的类似问题的链接

请帮我解决这个问题。

最佳答案

是的,您可以在 Twitter 上发布图片。

AIK,上传照片到推特有两种方法。

使用首先,您必须实现 Twitter API 并使用 this Link将 Photot 上传到 Twitter。

对不起这个例子。因为我没有得到有关如何使用它的任何示例。

借助 Second,您可以在 twitPic4j API 的帮助下完成此操作。 只需添加 twitPic4j 的 API 并编写以下代码即可上传照片。

代码:

File picture = new File(APP_FILE_PATH + "/"+filename+".jpg");  
// Create TwitPic object and allocate TwitPicResponse object 
TwitPic tpRequest = new TwitPic(TWITTER_NAME, TWITTER_PASSWORD); 
TwitPicResponse tpResponse = null;  
// Make request and handle exceptions                            
try {         
        tpResponse = tpRequest.uploadAndPost(picture, customMessageEditText.getText()+" http://www.twsbi.com/");

} 
catch (IOException e) {         
        e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), "Please enter valid username and password.", Toast.LENGTH_SHORT).show();
} 
catch (TwitPicException e) {         
        e.printStackTrace(); 
        Toast.makeText(getApplicationContext(), "Invalid username and password.", Toast.LENGTH_SHORT).show();
        Toast.makeText(getApplicationContext(), "Please enter valid Username and Password.", Toast.LENGTH_SHORT).show();
}  
// If we got a response back, print out response variables                               
if(tpResponse != null) {         
       tpResponse.dumpVars();
       System.out.println(tpResponse.getStatus());
       if(tpResponse.getStatus().equals("ok")){
            Toast.makeText(getApplicationContext(), "Photo posted on Twitter.",Toast.LENGTH_SHORT).show();
            //picture.delete();
       }
 }

以上代码适用于我的情况。

希望你得到了第二个的解决方案,我不知道如何使用第一个。

享受吧。 :)

已更新

如果它仍然不适合您,请尝试下面列出的一些项目:

Example 1

Example 2

Example 3

Example 4

希望对你有所帮助。

快乐编码。

==================================

FOR erdomester 和更新的答案

==================================

请检查我在 Example1 及其 api 中给出的第一个链接:Twitter4J 因此,如果任何库停止提供在 Twitter 上上传图片的功能,您可以使用其他库。请检查并阅读 regrading Twitter4j。​​

检查 Twitter4J API 以将文件上传到 Twitter:Twitter4j Image Upload

例如帮助你也可以检查下面的代码在 Twitter 上上传图像文件。

上传图片代码:

    /**
 * To upload a picture with some piece of text.
 * 
 * 
 * @param file The file which we want to share with our tweet
 * @param message Message to display with picture
 * @param twitter Instance of authorized Twitter class
 * @throws Exception exception if any
 */

public void uploadPic(File file, String message,Twitter twitter) throws Exception  {
    try{
        StatusUpdate status = new StatusUpdate(message);
        status.setMedia(file);
        twitter.updateStatus(status);}
    catch(TwitterException e){
        Log.d("TAG", "Pic Upload error" + e.getErrorMessage());
        throw e;
    }
}

希望对您的查询有所帮助。

感谢 eredomester 通知我 tweetpic 不再适用于 Twitter。但是在您没有完全搜索给定的回复之前,请不要对答案投反对票。 Example1 中给定的库 Twitter4J 给出了将图像上传到 twitter 的清晰思路,您可以轻松实现它。

如需更多帮助和代码,您还可以查看:Twitter Upload media

注意:要使用它,请确保您有最新的 jar 文件。我用过twitter4j-core-2.2.5.jar或更多。

如果您在这方面遇到任何问题,请评论我而不是否决这个答案。

关于android - 我们可以在 Android 中使用 twitter API 在 twitter 上发布图片吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7609656/

相关文章:

mysql - 将 Twitter 值插入 MYSQL 数据库时出现 "Exeception occured:must be str"错误

android-如何触发通知onclick事件?或者如何仅在用户点击时获取数据

Android SQLite 数据库和日志权限

java - 将我的数据库移动到 SD 卡不起作用

python - 通过 Tweepy 获取 Twitter 中的所有关注者 ID

php - jquery twitter 插件,对 seo 友好

android - 在 RecyclerView 中更新 ProgressBar

android - 使用 JRebel for Android 即时部署 Nativescript

ios - 图片和媒体未在 TWTRTweetTableViewCell TwitterKit ios 中显示

ruby-on-rails - Rails 3 - 如何跳过验证规则?