Android twitter tweet with image [duplicate]

2020-02-26 00:50发布

Possible Duplicate:
Can we post image on twitter using twitter API in Android?

I am working in an android application and I want to tweet a message and a picture to twitter. I am able to tweet only tweets to twitter by the code :

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

AccessToken a = new AccessToken(token, secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY,
        Constants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
try {

**twitter.updateStatus("New tweet");**
        twitter.//Which property of twitter should I use to tweet an image and  //message
} catch (TwitterException e) {
    // TODO Auto-generated catch block
    Log.e("Errorssssssssssssss", e.toString());
}

How do I include an image as well?

标签: android
2条回答
倾城 Initia
2楼-- · 2020-02-26 00:55

refer to http://www.londatiga.net/it/how-to-post-twitter-status-from-android/, use twitter4j library

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

where mTwitter is an instance of Twitter class

Make sure you are using latest version of twitter4j-core jar file.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-02-26 00:59

U can try example which comes with Twitter4j Library.Following code will help u

public final class TwitpicImageUpload {
    /**
     * Usage: java twitter4j.examples.media.TwitpicImageUpload [API key] [message]
     *
     * @param args message
     */
    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Usage: java twitter4j.examples.media.TwitpicImageUpload [API key] [image file path] [message]");
            System.exit(-1);
        }
        try {
            Configuration conf = new ConfigurationBuilder().setMediaProviderAPIKey(args[0]).build();
            ImageUpload upload = new ImageUploadFactory(conf).getInstance(MediaProvider.TWITPIC);
            String url;
            if (args.length >= 3) {
                url = upload.upload(new File(args[1]), args[2]);
            } else {
                url = upload.upload(new File(args[1]));
            }
            System.out.println("Successfully uploaded image to Twitpic at " + url);
            System.exit(0);
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to upload the image: " + te.getMessage());
            System.exit(-1);
        }
    }
}

Download Twitter4j Library look for more examples there.

查看更多
登录 后发表回答