user1891910
user1891910

Reputation: 909

How to upload Image to twitter using update_with_media twitter api in android?

https://api.twitter.com/1.1/statuses/update_with_media.json

After doing a lots of research on how to post image on twitter,I got this url to post image to twitter.But I didnt know how to use this and what parameters I need to pass.

I also found that

1) Posting to Twitpic is one of the way to post image to twitter,but I didnt know how to use that twitpic library.

I am familiar with posting image to facebook using graph api (url and accesstoken),so I feel that posting image using the above API is much easier.

please suggest me in using the above api

Thanks in advance

Upvotes: 1

Views: 3026

Answers (2)

patient
patient

Reputation: 41

Have look at this tutorial it solved my whole proble Twit image along text

Upvotes: 1

Dhaval Parmar
Dhaval Parmar

Reputation: 18978

check below code: you have to use use twitter4j-core-2.1.11.jar & twitpic4j.jar

before use this checkthispost.

Login in to twitter using above android code.

You must have to login in twitter and use OAUTH_TOKEN & OAUTH_TOKEN_SECRET in this method. in this i have passed URL same way you can also pass bitmap.

private void hello() {
        // TODO Auto-generated method stub
        String url;

        long result = 0;

        String oth = prefs.getString(OAuth.OAUTH_TOKEN, "");
        String src = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

        Configuration conf = new ConfigurationBuilder()
                .setOAuthConsumerKey(Constants.CONSUMER_KEY)
                .setOAuthConsumerSecret(Constants.CONSUMER_SECRET)
                .setOAuthAccessToken(oth).setOAuthAccessTokenSecret(src)
                .build();

        OAuthAuthorization auth = new OAuthAuthorization(conf,
                conf.getOAuthConsumerKey(), conf.getOAuthConsumerSecret(),
                new AccessToken(conf.getOAuthAccessToken(),
                        conf.getOAuthAccessTokenSecret()));

        ImageUpload upload = ImageUpload.getTwitpicUploader(
                Constants.twitpic_api_key, auth);

        Log.d(main_genral_class.TAG, "Start sending image...");

        try {
                url = upload.upload(" ", new URL("https://i.sstatic.net/wz0qZ.jpg").openStream(),
                        "some text");
            
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

url is give you URL of uploaded image

for bitmap you can use like : upload.upload(String, inputstream);

Edited

I have checked my code and replace twitter4j-core-2.1.11.jar to twitter4j-core-3.0.3.jar

check full code here

and also change some setting in Twitter App on https://dev.twitter.com

enter image description here

enter code here

its working fine....

Upvotes: 2

Related Questions