是否可以使用 twitter api 将图像发布到 twitter?
如果可能的话,请提供一些关于此的信息。
谢谢
您需要 Twitter4J Library for Sharing Pic,这应该是最新的,可以从这里获取
并且可以使用下面的方法来post Image
**
* 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;
}
}
试试这个:Android 演示。愿它有帮助。
if(isConnected()){
OAuthService authService = new ServiceBuilder()
.provider(TwitterApi.class)
.apiKey(Constants.CONSUMER_KEY)
.apiSecret(Constants.CONSUMER_SECRET)
.callback(Constants.OAUTH_CALLBACK_URL)
.build();
final OAuthRequest request = new OAuthRequest(Verb.POST, "https://upload.twitter.com/1/statuses/update_with_media.json");
verifier = uri.getQueryParameter(Constants.IEXTRA_OAUTH_VERIFIER);
try {
Token token = getToken();
if(token!=null){
authService.signRequest(token, request);
MultipartEntity entity = new MultipartEntity();
entity.addPart("status", new StringBody("Your Status!")); // THIS IS THE TWITTER MESSAGE
entity.addPart("media", new FileBody(new File(uriString))); // THIS IS THE PHOTO TO UPLOAD
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
request.addPayload(out.toByteArray());
request.addHeader(entity.getContentType().getName(), entity.getContentType().getValue());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
progressDialog = ProgressDialog.show(this,Constants.LABEL_BLANK, "Uploading Image",true);
Thread requestThread = new Thread() {
public void run() {
try {
response = new JSONObject (request.send().getBody());
uiHandler.sendEmptyMessage(0);
}catch (JSONException e) {
Log.e("TOUR_APP_TAG", "JSONException Thrown: " + e.getMessage());
errorHandler.sendEmptyMessage(0);
}
}
};
requestThread.start();
}
...
...
final Handler uiHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(progressDialog != null)progressDialog.dismiss();
if(response.has("id"))
//your code
}
};
final Handler errorHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(progressDialog != null)progressDialog.dismiss();
//your code
}
};