我们可以在Android上使用Twitter API在Twitter上发布图片吗?

Dha*_*dra 13 twitter android

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

  1. 是否可以在Android上使用API​​在Twitter上发布图像?

  2. 在twitter中我们使用OAuth.OAUTH_TOKENOAuth.OAUTH_TOKEN_SECRET令牌.我在下面的代码中的第二个参数上传递令牌值是否可以?或者我必须留空?

    String token = prefs.getString(OAuth.OAUTH_TOKEN, OAuth_token_key);
    String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, OAuth_token_secret);
    
    Run Code Online (Sandbox Code Playgroud)

我搜索知道是否可能,post image on twitter using twitter API in Android但我没有找到任何我知道是否可能的链接.

我有一个类似的iPhone后期图像问题,也有一个答案.我不知道iPhone,所以我不知道天气是否正确答案.这是一个类似的iPhone邮政形象问题的链接

请帮我解决这个问题.

Shr*_*jan 15

是的您可以在Twitter上发布图像.

AIK,有两种方法可以将照片上传到Twitter.

随着首先,你必须implemente Twitter的API,并使用此链接到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();
       }
 }
Run Code Online (Sandbox Code Playgroud)

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

希望你得到第二个溶剂,我不知道如何使用第一个.

请享用.:)

更新

如果它仍然不适合您并尝试下面列出的项目:

例1

例2

例3

例4

希望能帮到你.

快乐的编码.

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

为erdomester和更新的答案

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

请检查我给Example1给出的第一个链接及其api:Twitter4J 所以,如果任何库停止提供在twitter上传图像的功能,你可以使用其他库.请检查并阅读重新分类Twitter4j.

检查Twitter4J API将文件上传到Twitter:Twitter4j图片上传

例如帮助您还可以检查以下代码以在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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助您更多地查询.

感谢eredomester通知我,tweetpic不再适用于Twitter.但请不要做downvote回答,直到你没有完全搜索给定的答复.鉴于图1中的库Twitter4J给出了关于将图像上传到twitter的清晰想法,您可以轻松实现它.

有关更多帮助和代码,您还可以检查:Twitter上传媒体

注意: 要使用此功能,请确保您拥有最新的jar文件.我已经使用了twitter4j-core-2.2.5.jar或更多.

如果你遇到任何问题,请评论我而不是低估这个答案.