在我的Android应用程序中,我需要将图像上传到twitpic。我的SD卡中有一些图像,想在点击按钮后上传到twitpic。这是我的代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
int userID = oAuthAccessToken.getUserId();
User user = twitter.showUser(userID);
username = user.getName();
}
catch (Exception e) {
// TODO: handle exception
}
}
}
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;
TwitterSession twitterSession = new TwitterSession(MainActivity.this);
twitterSession.storeAccessToken(oAuthAccessToken, username);
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 {
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/Friends/"+"/image2.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.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();
}
}
这是我的TwitterSession类
public class TwitterSession {
private SharedPreferences sharedPref;
private Editor editor;
private static final String TWEET_AUTH_KEY = "auth_key";
private static final String TWEET_AUTH_SECRET_KEY = "auth_secret_key";
private static final String TWEET_USER_NAME = "user_name";
private static final String SHARED = "Twitter_Preferences";
private static Twitter twitter;
public TwitterSession(Context context) {
sharedPref = context.getSharedPreferences(SHARED, Context.MODE_PRIVATE);
editor = sharedPref.edit();
}
public void storeAccessToken(AccessToken accessToken, String username) {
editor.putString(TWEET_AUTH_KEY, accessToken.getToken());
editor.putString(TWEET_AUTH_SECRET_KEY, accessToken.getTokenSecret());
editor.putString(TWEET_USER_NAME, username);
editor.commit();
}
public void resetAccessToken() {
editor.putString(TWEET_AUTH_KEY, null);
editor.putString(TWEET_AUTH_SECRET_KEY, null);
editor.putString(TWEET_USER_NAME, null);
editor.commit();
}
public String getUsername() {
return sharedPref.getString(TWEET_USER_NAME, "");
}
public AccessToken getAccessToken() {
String token = sharedPref.getString(TWEET_AUTH_KEY, null);
String tokenSecret = sharedPref.getString(TWEET_AUTH_SECRET_KEY, null);
if (token != null && tokenSecret != null)
return new AccessToken(token, tokenSecret);
else
return null;
}
这是一个NullPointer异常。
editor.putString(TWEET_AUTH_KEY, accessToken.getToken());
我的日志是
03-31 05:09:20.178:E / AndroidRuntime(1075):致命异常:AsyncTask#1 03-31 05:09:20.178:E / AndroidRuntime(1075):java.lang.RuntimeException:执行doInBackground()时发生错误 03-31 05:09:20.178:E / AndroidRuntime(1075):在android.os.AsyncTask $ 3.done(AsyncTask.java:299) 03-31 05:09:20.178:E / AndroidRuntime(1075):at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 03-31 05:09:20.178:E / AndroidRuntime(1075):at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 03-31 05:09:20.178:E / AndroidRuntime(1075):at java.util.concurrent.FutureTask.run(FutureTask.java:239) 03-31 05:09:20.178:E / AndroidRuntime(1075):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) 03-31 05:09:20.178:E / AndroidRuntime(1075):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 03-31 05:09:20.178:E / AndroidRuntime(1075):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 03-31 05:09:20.178:E / AndroidRuntime(1075):at java.lang.Thread.run(Thread.java:856) 03-31 05:09:20.178:E / AndroidRuntime(1075):引起:java.lang.NullPointerException 03-31 05:09:20.178:E / AndroidRuntime(1075):at com.my.androidtwitpicapplication.TwitterSession.storeAccessToken(TwitterSession.java:36) 03-31 05:09:20.178:E / AndroidRuntime(1075):at com.my.androidtwitpicapplication.MainActivity $ ImageSender.doInBackground(MainActivity.java:91) 03-31 05:09:20.178:E / AndroidRuntime(1075):at com.my.androidtwitpicapplication.MainActivity $ ImageSender.doInBackground(MainActivity.java:1) 03-31 05:09:20.178:E / AndroidRuntime(1075):在android.os.AsyncTask $ 2.call(AsyncTask.java:287) 03-31 05:09:20.178:E / AndroidRuntime(1075):at java.util.concurrent.FutureTask.run(FutureTask.java:234)
我认为调用public void storeAccessToken(AccessToken accessToken, String username)
方法时存在问题,但无法解决问题。请帮我解决问题。感谢
答案 0 :(得分:0)
catch (Exception e) {
// TODO: handle exception
}
如果此处发现了异常,那么oAuthAccessToken
可能是null
,这会导致storeAccessToken
抛出NullPointerException
。您需要确保执行更多操作来处理该异常(包括不使用null storeAccessToken
调用AccessToken
)。