Can we post image on twitter using twitter API in Android?

Yes You can post the Image on the Twitter.

AIK, there are two methods to upload the photo to the Twitter.

With First you have to implemente the Twitter API and use this Link to upload the Photot to the Twitter.

Sorry for the Example. as i dont get any example for how to use this.

With Second you can do this with the help of the twitPic4j API.
Just add the API for twitPic4j and write below code to upload the photo.

Code:

File picture = new File(APP_FILE_PATH + "https://stackoverflow.com/"+filename+".jpg");  
// Create TwitPic object and allocate TwitPicResponse object 
TwitPic tpRequest = new TwitPic(TWITTER_NAME, TWITTER_PASSWORD); 
TwitPicResponse tpResponse = null;  
// Make request and handle exceptions                            
try {         
        tpResponse = tpRequest.uploadAndPost(picture, customMessageEditText.getText()+" http://www.twsbi.com/");

} 
catch (IOException e) {         
        e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), "Please enter valid username and password.", Toast.LENGTH_SHORT).show();
} 
catch (TwitPicException e) {         
        e.printStackTrace(); 
        Toast.makeText(getApplicationContext(), "Invalid username and password.", Toast.LENGTH_SHORT).show();
        Toast.makeText(getApplicationContext(), "Please enter valid Username and Password.", Toast.LENGTH_SHORT).show();
}  
// If we got a response back, print out response variables                               
if(tpResponse != null) {         
       tpResponse.dumpVars();
       System.out.println(tpResponse.getStatus());
       if(tpResponse.getStatus().equals("ok")){
            Toast.makeText(getApplicationContext(), "Photo posted on Twitter.",Toast.LENGTH_SHORT).show();
            //picture.delete();
       }
 }

Above code works in for my case.

Hope you got the sollution with the second one and i dont know how to use the first one.

Enjoy. 🙂

Updated

If still it not works for you and try some project listed below:

Example 1

Example 2

Example 3

Example 4

Hope that will help you.

Happy Coding.

==================================

FOR erdomester and Updated answer

==================================

Please check my first link given with Example1 and its api: Twitter4J
So, if any library that stop giving functionality to upload image on twitter, you can use other library for same. Please check and read regrading Twitter4j.

Check Twitter4J API to upload file to Twitter: Twitter4j Image Upload

For instance help you can also check below code to upload image file on Twitter.

Code to upload Image:

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

I hope this will help you more for your query.

Thanks to eredomester to notify me that tweetpic is no more working for Twitter. But please dont do downvote to answer untill you have not fully search on the given reply. Given library Twitter4J in Example1 gives clear idea about uploading image to twitter and you can easily implement it.

For more help and code you can also check: Twitter Upload media

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

Please comment me instead of downvoting this answer, if you facing any issue in this.

Leave a Comment