7

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?

Community
  • 1
  • 1
Arun PS
  • 4,610
  • 6
  • 41
  • 56
  • Refer this question [stackoverflow.com/questions/7609656/can-we-post-image-on-twitter-using-twitter-api-in-android](http://stackoverflow.com/questions/7609656/can-we-post-image-on-twitter-using-twitter-api-in-android) – Aerrow May 17 '12 at 11:52
  • try this http://stackoverflow.com/questions/17093499/how-to-post-image-to-twitter-in-android/20633178#20633178 – Prachi Dec 23 '13 at 11:27

2 Answers2

12

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.

Akram
  • 7,548
  • 8
  • 45
  • 72
Gaurav Vashisth
  • 7,547
  • 8
  • 35
  • 56
  • status.setMedia(file); Hi. I am not able to get a property named setMedia.Please help me – Arun PS May 22 '12 at 07:09
  • 2
    use twitter4j-core-2.2.5.jar, may be you are using old library – Gaurav Vashisth May 22 '12 at 07:19
  • Hi Gaurav Vashisth,I got the property setMedia in the jar file twitter4j-core-2.2.5.jar,but I am getting an error: Could not find class 'twitter4j.auth.AccessToken', referenced from method android.twitter.TwitterUtils. This is the code that I used to authenticate AccessToken accesstoken = new AccessToken(token, secret); Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET); twitter.setOAuthAccessToken(accesstoken); – Arun PS May 23 '12 at 04:43
  • check this http://stackoverflow.com/questions/10005206/twitter4j-androidruntime446-java-lang-noclassdeffounderror-twitter4j-http – Gaurav Vashisth May 23 '12 at 04:52
  • Hi Gaurav Vashisth ,I got the property setMedia in the jar file twitter4j-core-2.2.5.jar, but now I am getting an error :AndroidRuntime(694): Caused by: java.lang.IllegalArgumentException: Invalid access token format in the same code posted. Please help me – Arun PS May 23 '12 at 09:29
  • check the access (read, write) in the twitter app – Gaurav Vashisth May 23 '12 at 10:18
  • yes..it read, write. It was already working. But when I changed the jar file higher version it caused the error.Please check the link http://osdir.com/ml/twitter4j/2011-03/msg00289.html – Arun PS May 23 '12 at 10:19
  • Please look this too http://twitter4j.org/en/versions.html#migration21x-22x – Arun PS May 23 '12 at 10:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11635/discussion-between-arun-and-gaurav-vashisth) – Arun PS May 23 '12 at 10:33
  • @Arun Have you used twitpic to upload the image?? can you give a link to your code? – hemanth kumar Aug 31 '12 at 05:48
  • I have used twitter4j-core-2.2.5.jar to upload images to Twitter and the above code works fine in doing this. – Arun PS Aug 31 '12 at 07:33
  • sir can you send the complete code how to send image and message both on twitter. – Naveen Kumar Oct 16 '12 at 11:46
  • check the link given in the answer – Gaurav Vashisth Oct 17 '12 at 16:11
  • Thanks Brother i can give +100000 – Zala Janaksinh Mar 19 '13 at 06:31
  • it post as a link. I want to share the image as such. I tried to work with this link https://dev.twitter.com/docs/api/1/post/statuses/update_with_media but It didnt worked. do you know how to use this API? – Manikandan May 23 '13 at 14:40
2

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.