如何从应用程序后台将媒体上传到 Twitter,而不向用户显示任何 UI 一次(登录屏幕除外)?
How to upload media to twitter from background of application without showing any UI to user except login screen once?
我试过访问推特developer website
但是我没有从那里得到正确的答案,因为我尝试了不同的方法来解决这个问题,但我确实发现 TweetComposer.Builder
仍然无法使用它。
我找到了我的问题的解决方案我已经从 here 下载并集成了一个库,借助这个库我能够 post 多媒体
第 1 步:
首先根据指导
从这个link集成库来登录twitter
第 2 步:
之后您将能够使用 Twitter 登录,您将得到两件事 1)authToken.token
和 2)authToken.secret
存储它以供进一步使用。
第 3 步:
public void updateTwitterStatus() {
new AsyncTask() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(Object[] params) {
try {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(getResources().getString("YOUR_TWITTER_CONSUMER_KEY"));
builder.setOAuthConsumerSecret(getResources().getString("YOUR_TWITTER_CONSUMER_SECRET"));
// Access Token
String access_token = sharedPreferences.getString("authToken.token", "");
// Access Token Secret
String access_token_secret = sharedPreferences.getString("authToken.secret", "");
twitter4j.auth.AccessToken accessToken = new twitter4j.auth.AccessToken(access_token, access_token_secret);
twitter4j.Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
// Update status for single image upload with raw folder
StatusUpdate statusUpdate = new StatusUpdate("posted from my app");
InputStream is = getResources().openRawResource(R.raw.lakeside_view);
statusUpdate.setMedia("test.jpg", is);
// Update status for single image upload with file
/*String statusMessage = "posted from my app";
StatusUpdate statusUpdate = new StatusUpdate(statusMessage);
statusUpdate.setMedia(file);*/
// Update status for multiple images upload with file
/*String statusMessage = "Hey I am posting 2 images of xyz event";
File imagefile1 = new File(Environment.getExternalStorageDirectory()+"/images/image_1.jpg");
File imagefile2 = new File(Environment.getExternalStorageDirectory()+"/images/image_2.jpg");
long[] mediaIds = new long[2];
UploadedMedia media1 = twitter.uploadMedia(imagefile1);
mediaIds[0] = media1.getMediaId();
UploadedMedia media2 = twitter.uploadMedia(imagefile2);
mediaIds[1] = media2.getMediaId();
StatusUpdate statusUpdate = new StatusUpdate(statusMessage);
statusUpdate.setMediaIds(mediaIds);*/
// to post twit
twitter4j.Status response = twitter.updateStatus(statusUpdate);
Log.d("Status", response.getText());
} catch (twitter4j.TwitterException e) {
Log.d("Failed to post!", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Object o) {
*//* Dismiss the progress dialog after sharing *//*
Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT).show();
super.onPostExecute(o);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
我试过这段代码对我有用。
我试过访问推特developer website
但是我没有从那里得到正确的答案,因为我尝试了不同的方法来解决这个问题,但我确实发现 TweetComposer.Builder
仍然无法使用它。
我找到了我的问题的解决方案我已经从 here 下载并集成了一个库,借助这个库我能够 post 多媒体
第 1 步:
首先根据指导
从这个link集成库来登录twitter第 2 步:
之后您将能够使用 Twitter 登录,您将得到两件事 1)authToken.token
和 2)authToken.secret
存储它以供进一步使用。
第 3 步:
public void updateTwitterStatus() {
new AsyncTask() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(Object[] params) {
try {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(getResources().getString("YOUR_TWITTER_CONSUMER_KEY"));
builder.setOAuthConsumerSecret(getResources().getString("YOUR_TWITTER_CONSUMER_SECRET"));
// Access Token
String access_token = sharedPreferences.getString("authToken.token", "");
// Access Token Secret
String access_token_secret = sharedPreferences.getString("authToken.secret", "");
twitter4j.auth.AccessToken accessToken = new twitter4j.auth.AccessToken(access_token, access_token_secret);
twitter4j.Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
// Update status for single image upload with raw folder
StatusUpdate statusUpdate = new StatusUpdate("posted from my app");
InputStream is = getResources().openRawResource(R.raw.lakeside_view);
statusUpdate.setMedia("test.jpg", is);
// Update status for single image upload with file
/*String statusMessage = "posted from my app";
StatusUpdate statusUpdate = new StatusUpdate(statusMessage);
statusUpdate.setMedia(file);*/
// Update status for multiple images upload with file
/*String statusMessage = "Hey I am posting 2 images of xyz event";
File imagefile1 = new File(Environment.getExternalStorageDirectory()+"/images/image_1.jpg");
File imagefile2 = new File(Environment.getExternalStorageDirectory()+"/images/image_2.jpg");
long[] mediaIds = new long[2];
UploadedMedia media1 = twitter.uploadMedia(imagefile1);
mediaIds[0] = media1.getMediaId();
UploadedMedia media2 = twitter.uploadMedia(imagefile2);
mediaIds[1] = media2.getMediaId();
StatusUpdate statusUpdate = new StatusUpdate(statusMessage);
statusUpdate.setMediaIds(mediaIds);*/
// to post twit
twitter4j.Status response = twitter.updateStatus(statusUpdate);
Log.d("Status", response.getText());
} catch (twitter4j.TwitterException e) {
Log.d("Failed to post!", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Object o) {
*//* Dismiss the progress dialog after sharing *//*
Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT).show();
super.onPostExecute(o);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
我试过这段代码对我有用。