Home » Android » Android twitter4j upload image

Android twitter4j upload image

Posted by: admin May 14, 2020 Leave a comment

Questions:

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?

How to&Answers:

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