是的,您可以在 Twitter 上发布图片。
AIK,有两种方法可以将照片上传到 Twitter。
首先您必须实现 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 上上传图像的功能,您可以使用其他库来实现相同的功能。请检查并阅读重新分级 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 或更多。
如果您在这方面遇到任何问题,请评论我而不是投反对票。