Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I had successfully integrate twitter API and I am able to post text from my device but I want to know two things

  1. Is is possible to post Image on twitter using API in Android ?

  2. In twitter we used OAuth.OAUTH_TOKEN and OAuth.OAUTH_TOKEN_SECRET tokens.I passing token values on second argument in below code is it ok ? or I have to leave it blank ?

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

I searched to know whether is it possible to post image on twitter using twitter API in Android but I have not found any link that I know whether it is possible or not.

I got one similar post image issue for iPhone and there is a answer also. I don't know about iPhone so I can't know weather it is right answer or not.Here is a link of similar question of post image for iPhone

Please help me out from this issue.

like image 894
Dharmendra Avatar asked Sep 30 '11 11:09

Dharmendra


1 Answers

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 + "/"+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.

like image 194
Shreyash Mahajan Avatar answered Oct 15 '22 17:10

Shreyash Mahajan