Twitpic is one of the most common used image hosting that allows users to easily post images to Twitter and other social media. Twitpic can be used independently of Twitter, in a way similar to Google Picasa or Flickr. TwitPic usernames and passwords are the same as the ones in Twitter so it doesn’t have to create new account on Twitpic.
As many other social media, Twitpic also provides RESTful API to enable developers to post image to Twitpic. The current version at the time of this writing is version 2 (V2). Using this API, we can build an Android application that features an image upload to Twitpic so we don’t have to create a new infrastructure for image hosting server.
In this tutorial i’ll explain how to send image to Twitpic from an Android application using Twitter4j library. The source code for this tutorial can be downloaded from my github page (see the download link at the bottom of this post).
I. Register Application
To enable an Android application to send image to Twitpic, first you have to register your application to get an API Key. To get an API Key:



II. Android Integration
In my sample project i use Twitter4j library to send image to Twitpic. You can download the latest version from Twitter4j download page or use the library package i’ve provided in this tutorial. To enable Android to send image to Twitpic, first you have to connect to Twitter first using a Twitter account. How to connect to Twitter will not be explained here, you can read my previous tutorial on how to post Twitter status from Android. So basically to be enable to send image to Twitpic, first you have to sign in to Twitter, get the access token and use the access token along with Twitter consumer key, secret key and Twitpic API key.
In my sample project, i create two packages. The first is for Twitter library, which can be found also on my previous Twitter tutorial sample project and the second is the main application page.
Connect to Twitter (ConnectActivity.java)
This activity displays the Twitter login page and authenticates user to get the access token. More comprehensive explanation can be found on my previous tutorial.

Send Image to Twitpic (SendImageActivity.java)
In this activity, using an image picker, the image to be sent can be from existing images on sdcard or directly taking a picture from camera. You can read my previous tutorial on how to create image picker on Android to get more deep understanding on how it works. The classes from Twitter4j that are used to send image to Twitpic are:
import twitter4j.conf.Configuration; import twitter4j.conf.ConfigurationBuilder; import twitter4j.http.AccessToken; import twitter4j.http.OAuthAuthorization; import twitter4j.util.ImageUpload;
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;
TwitterSession twitterSession = new TwitterSession(SendImageActivity.this);
AccessToken accessToken = twitterSession.getAccessToken();
Configuration conf = new ConfigurationBuilder()
.setOAuthConsumerKey(twitter_consumer_key)
.setOAuthConsumerSecret(twitter_secret_key)
.setOAuthAccessToken(accessToken.getToken())
.setOAuthAccessTokenSecret(accessToken.getTokenSecret())
.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));
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();
}
}
In this example, i use asynchronous task to send image to Twitpic, you can use more convenience way to send the image such as using background service with queue to manage uploading multiple images. The main code for sending image to Twitpic defined in doInBackground() method.
line 14: create an object of TwitterSession class to get the accsess token that was previously saved when user was successfully authenticated.
line 15: get the access token object.
line 16: create the configuration object with Twitter consumer key, secret key, access token and access token secret as parameters.
line 24: create the authorization object.
line 27: create the Twitpic image uploader object.
line 32: upload the image file to Twitpic, the method uses file path to the image that was selected from image picker.
lin3 35: if success, the upload method will return the Twitpic url to the uploaded image, ex: http://twitpic.com/6hqd44

Download
Related post:
Thanks i solved it
hey,how to send image to twitter from android i’m using json and oauth.
have you read this post?;)