10

I have an android app through which I can successfully update the twitter status. I am using it to share links to a webpage which displays an image. I think it will be nice to have a scaled down preview of the image along with the tweet the same way instagram does. How do I upload this image along with my tweet?

Rameez Hussain
  • 6,414
  • 10
  • 56
  • 85
  • try this for updated twitter4j jar http://stackoverflow.com/questions/17093499/how-to-post-image-to-twitter-in-android/20633178#20633178 – Prachi Dec 23 '13 at 11:28

1 Answers1

43

I had the same requirement sometimes back and finally come with the function may be it will help you too.

/**
 * 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;
    }
}

Note: To use this please make sure you have latest jar file i have used twitter4j-core-2.2.5.jar for this

Akram
  • 7,548
  • 8
  • 45
  • 72
  • 5
    Nice! Thanks a lot. Worked perfectly! :) – Rameez Hussain Oct 05 '12 at 09:08
  • after replacing my twitter jar from 2.i.i to 2.2.5 as you suggested i am getting new error : AndroidRuntime(7452): java.lang.IllegalArgumentException: Invalid access token format. previously i was able to tweet but only for image upload i was trying that. – mrYogi Feb 23 '13 at 13:48
  • Please what is Twitter twitter – yakusha Jun 04 '13 at 06:13
  • 1
    Twitter is an Object used from Twitter4j Library – Salman Khakwani Jul 10 '13 at 13:06
  • 1
    How 'bout if I wanted to share a link? – KarenAnne Aug 21 '13 at 02:16
  • This doesnt work form me... Please help me.. I want to upload image with status on twitter .. I am able to update status. But when.i use status.setMedia(file); it give me error StatusUpdate status = new StatusUpdate(message); status.setMedia(file); //twitter.uploadMedia(file); twitter.updateStatus(status); – ADT Jul 11 '14 at 05:36
  • if you want get the id of posted tweet. getId(); doesnt work for statusupdate. please avoid this.. when you require id of tweeted tweet. – Sree Reddy Menon Nov 15 '14 at 16:24