成功登录完成后。
public class ShareOnTwitterTrophy extends AsyncTask<String, Integer, Long> {
private Activity mActivity;
private Bitmap bitmap;
public ShareOnTwitterTrophy(Activity mActivity,Bitmap bitmap) {
this.mActivity=mActivity;
this.bitmap=bitmap;
}
protected void onPreExecute() {
}
@Override
protected Long doInBackground(String... arg0) {
long result = 0;
// TwitterSession twitterSession = new TwitterSession(activity);
// AccessToken accessToken = twitterSession.getAccessToken();
AccessToken accessToken = new UserSharedPreference(mActivity).getTwitterAccessToken();
if (accessToken != null) {
Configuration conf = new ConfigurationBuilder()
.setOAuthConsumerKey("your key")
.setOAuthConsumerSecret(
"your secret")
.setOAuthAccessToken(accessToken.getToken())
.setOAuthAccessTokenSecret(accessToken.getTokenSecret())
.build();
ImageUploadFactory factory = new ImageUploadFactory(conf);
ImageUpload upload = factory.getInstance();
Log.d("", "Start sending image...");
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
// you can create a new file name "test.jpg" in sdcard
// folder.
String imagePath = Environment
.getExternalStorageDirectory()
+ File.separator
+ "test.jpg";
File f = new File(imagePath);
f.createNewFile();
// write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
upload.upload(f, "");
Log.e("Image Uploaded", "yayeeeee");
result = 1;
} catch (Exception e) {
Log.e("image upload failed", "awwwww :(");
e.printStackTrace();
}
return result;
}
return result;
}
@Override
protected void onPostExecute(Long result) {
if (result == 1)
Toast.makeText(
mActivity,
mActivity
.getString(R.string.twitter_shared_successfully),
Toast.LENGTH_LONG).show();
}