我需要将图片上传到twitpic。在我的SD卡目录中有一些图像,我必须上传到twitpic。现在我想从他们上传一张图片。这是我的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPref = getApplicationContext().getSharedPreferences(
"MyPref", 0);
editor = sharedPref.edit();
uploadImage = (Button) findViewById(R.id.uploadImage);
uploadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
new ImageSender().execute();
}
});
uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {
// oAuth verifier
String verifier = uri
.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);
try {
// Get the access token
oAuthAccessToken = twitter.getOAuthAccessToken(
verifier);
editor.putString(PREF_KEY_OAUTH_TOKEN, oAuthAccessToken.getToken());
editor.putString(PREF_KEY_OAUTH_SECRET,
oAuthAccessToken.getTokenSecret());
editor.commit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
private class ImageSender extends AsyncTask<URL, Integer, Long> {
private String url;
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(MainActivity.this, "", "Sending image...", true);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
protected Long doInBackground(URL... urls) {
long result = 0;
Configuration conf = new ConfigurationBuilder()
.setOAuthConsumerKey(twitter_consumer_key)
.setOAuthConsumerSecret(twitter_secret_key)
.setOAuthAccessToken(sharedPref.getString(OAuth.OAUTH_TOKEN, ""))
.setOAuthAccessTokenSecret(sharedPref.getString(OAuth.OAUTH_TOKEN_SECRET, ""))
.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 {
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/Friends/"+"image4.jpg";
File targetDirector = new File(targetPath);
url = upload.upload(new File(targetDirector.getAbsolutePath()));
result = 1;
Log.d(TAG, "Image uploaded, Twitpic url is " + url);
} catch (Exception e) {
Log.e(TAG, "Failed to send image "+e);
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();
}
}
在我的代码的以下部分中,我在日志中收到一条消息
try {
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/Friends/"+"image4.jpg";
File targetDirector = new File(targetPath);
url = upload.upload(new File(targetDirector.getAbsolutePath()));
result = 1;
Log.d(TAG, "Image uploaded, Twitpic url is " + url);
} catch (Exception e) {
Log.e(TAG, "Failed to send image "+e);
e.printStackTrace();
}
这是我的日志
04-01 05:32:39.394:E / Tag(1438):发送图像失败写入错误:ssl = 0x2a19e750:系统调用期间的I / O错误,pipe pipeTwitterException {exceptionCode = [f69e96cd-132d5002 883a7a68-527cabed ],statusCode = -1,retryAfter = 0,rateLimitStatus = null,version = 2.1.6}
我该如何解决这个问题?感谢