2

Possible Duplicate:
Can we post image on twitter using twitter API in Android?
Android twitter tweet with image

i have to take picture from the camera and upload in user tweet status.I am unable to do please help. I have used followig code to post text but unable to upload photo in bitmap to twiiter

 public void shareTwitter()
         {
             try {
                String token =  myPrefs.getString(FindFriends.PREF_KEY_OAUTH_TOKEN, "");
                String secret =  myPrefs.getString(FindFriends.PREF_KEY_OAUTH_SECRET, "");

                    ConfigurationBuilder cb = new ConfigurationBuilder();
                    cb.setDebugEnabled(true)
                    .setOAuthConsumerKey(FindFriends.TWITTER_CONSUMER_KEY)
                    .setOAuthConsumerSecret(FindFriends.TWITTER_CONSUMER_SECRET)
                    .setOAuthAccessToken(token)
                    .setOAuthAccessTokenSecret(secret);
                    AccessToken accessToken = new AccessToken(token, secret);
                    Twitter twitter = new TwitterFactory(cb.build()).getInstance(accessToken);
                    twitter.updateStatus("hello");


             } catch (Exception e) {
                e.printStackTrace();
Community
  • 1
  • 1
  • similar question answered in another thread. http://stackoverflow.com/questions/10635113/android-twitter-tweet-with-image/10635325#10635325 – Gaurav Vashisth Nov 30 '12 at 05:47
  • @user1856402 Please see my answer it will solve your problem. – Dipak Keshariya Nov 30 '12 at 12:10
  • Hey refer the below link you can get your task done. [http://www.londatiga.net/it/how-to-send-image-to-twitpic-from-android/](http://www.londatiga.net/it/how-to-send-image-to-twitpic-from-android/) – TNR Nov 30 '12 at 04:53

3 Answers3

6

try this code hope this will You.

 Twitter twitter = new TwitterFactory(conf).getInstance();
            Bitmap bmp = BitmapFactory.decodeResource(
                    TwitterFriends.this.getResources(), R.drawable.edit_ic);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
            StatusUpdate status = new StatusUpdate(message);
            status.setMedia("newyear", bis);

            try {
                twitter.updateStatus(status);
            } catch (Exception e) {
                e.printStackTrace();
            }
Naveen Kumar
  • 3,738
  • 4
  • 29
  • 50
1

Twitter will update just status and not pictures. If you want to achieve then search for uploading images to TwitPic which will give you an bit.ly url of your image on TwitPic. Post the same url on Twitter which will redirect the user to Picture.

0

this is the upload button...

        upload.setOnClickListener(new OnClickListener()
        {   
            @Override
            public void onClick(View v) 
            {
                new ImageSender().execute();

            }
        });

and this is Async Task...

private class ImageSender extends AsyncTask<URL, Integer, Long> 
    {
        private String url;

        protected void onPreExecute() 
        {
            //mProgressDialog = ProgressDialog.show(SendImageActivity.this, "", "Sending image...", true);

            //mProgressDialog.setCancelable(false);
            //mProgressDialog.show();
        }

        protected Long doInBackground(URL... urls) 
        {            
            long result = 0;
            prefs = PreferenceManager.getDefaultSharedPreferences(TestingTwitterActivity.this);
            String token1=prefs.getString("token", null);
            String tokenSecret1=prefs.getString("tokenSecret", null);
            Configuration conf = new ConfigurationBuilder()                 
            .setOAuthConsumerKey(twitter_consumer_key) 
            .setOAuthConsumerSecret(twitter_secret_key) 
            .setOAuthAccessToken(token1) 
            .setOAuthAccessTokenSecret(tokenSecret1) 
            .build(); 

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

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

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

            try {
                url = upload.upload(new File(mPath));//here your camera pic file path...
                result = 1;

                //Log.d(TAG, "Image uploaded, Twitpic url is " + url);          
            } catch (Exception e) {        
                //Log.e(TAG, "Failed to send image");

                e.printStackTrace();
            }

            return result;
        }

        protected void onProgressUpdate(Integer... progress) 
        {            
        }

        protected void onPostExecute(Long result) 
        {
            //mProgressDialog.cancel();

            String text = (result == 1) ? "Image sent successfully.\n Twitpic url is: " + url : "Failed to send image";

            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
        }
    }
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39