2

I am trying to upload and share image using twitter4j library but unable to share this. can anyone please give me a working demo for image sharing on twitter that upload an image share on twitter.. i have searched a lot on google and Stackoverflow but not found suitable answer. I've tried with twitter4j 2.2.5 and 2.1.6 but no luck :( I'll be thankfull if anyone help me out.

Ishtiaq
  • 1,206
  • 9
  • 18

1 Answers1

2

I have solved.. auto import not working for me when i update library i used these libraries

  • twitter4j-core-3.0.2.jar
  • twitter4j-media-support-3.0.2.jar

these imports

import oauth.signpost.OAuth;
import twitter4j.StatusUpdate;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.media.ImageUpload;
import twitter4j.media.ImageUploadFactory;
import android.content.SharedPreferences;

and here is the code to share image on twitter

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

            AccessToken a = new AccessToken(token, secret);
            Twitter twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
            twitter.setOAuthAccessToken(a);

            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDebugEnabled(true).setOAuthConsumerKey(yourConsumerKey).setOAuthConsumerSecret(yourConsumerSeceret).setOAuthAccessToken(yourAccessToken).setOAuthAccessTokenSecret(yourAccessTokenSeceret);

            Configuration conf = cb.build();

            ImageUploadFactory factory = new ImageUploadFactory(conf);
            ImageUpload upload = factory.getInstance();
            String url = upload.upload(yourImage, yourCaptionWithImage); //image is a File Format and Caption is String

upload.upload update your twitter status with image

Ishtiaq
  • 1,206
  • 9
  • 18