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

后端 未结 3 1062
北海茫月
北海茫月 2020-12-03 05:34

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 usi

相关标签:
3条回答
  • 2020-12-03 06:06

    Yes you can post image on twitter using Twitter api like twitter4j but I will suggest you to do using HttpPost class and DefaultHttpClient class because its good in practice and you dont need to add any external twitter api to it.

    0 讨论(0)
  • 2020-12-03 06:25

    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.

    0 讨论(0)
  • 2020-12-03 06:29

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

    Yes you can upload Images to Twitter after successful Authentication Using Twitter Media Uplload.

    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 ?

    You should add both Token and Token Secret Key it will be useful for setTokenWithSecret methos of Tiwtter in which you have to send both Token and Token Secret..

    0 讨论(0)
提交回复
热议问题