Reputation: 81
I have tried upload a photo in twitter but the status is send not photo. Can anyone suggest some help?
Upvotes: 2
Views: 5986
Reputation: 2559
First you need to create an app on twitter
here is code to post message on twitter
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();
final Twitter twitter = new TwitterFactory(configuration).getInstance();
new Thread(new Runnable() {
private double x;
@Override
public void run() {
boolean success = true;
try {
x = Math.random();
twitter.updateStatus(message +" "+x);
} catch (TwitterException e) {
e.printStackTrace();
success = false;
}
final boolean finalSuccess = success;
callingActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
postResponse.onFinsihed(finalSuccess);
}
});
}
}).start();
check this tutorial for more details.
Upvotes: 1
Reputation: 11
Twitter4j 3.0.3 has updated its ImageUpload
class. ImageUpload.getTwitpicUploader
method is now deprecated; it has only ImageUpload.upload
method. Also, you have to add these two .jar on your projects build path:
You can get more info at: http://twitter4j.org/oldjavadocs/3.0.3/index.html
Upvotes: 1
Reputation: 72
Try this code..
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
String token = prefs.getString(OAuth.OAUTH_TOKEN, "") != null ? prefs
.getString(OAuth.OAUTH_TOKEN, "") : "";
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "") != null ? prefs
.getString(OAuth.OAUTH_TOKEN_SECRET, "") : "";
if (token != null && !token.trim().equals("") && secret != null
&& !secret.trim().equals("")) {
AccessToken a = new AccessToken(token, secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY,
Constants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
Configuration conf = new ConfigurationBuilder()
.setOAuthConsumerKey(Constants.CONSUMER_KEY)
.setOAuthConsumerSecret(Constants.CONSUMER_SECRET)
.setOAuthAccessToken(a.getToken())
.setOAuthAccessTokenSecret(a.getTokenSecret())
.build();
OAuthAuthorization auth = new OAuthAuthorization (conf, conf.getOAuthConsumerKey (), conf.getOAuthConsumerSecret (), new AccessToken (conf.getOAuthAccessToken (), AuthAccessTokenSecret ()));
ImageUpload upload = ImageUpload.getTwitpicUploader ("fa6707f43c41faab003dc134348acabd",auth);
url = upload.upload(new File(path));
twitter.updateStatus(url);
///Add twitter4j-core-2.1.11jar to libs
Upvotes: 2
Reputation: 30994
You need to upload the picture to a picture service like twitpic, which will return an URL, that you then put into your status update.
There are libraries like Twitter4J out there, that can help you with this task.
Have a look at Zwitscher on how to use it: code to upload
Upvotes: 2