-1
我試圖訪問推特developer website 但我沒有從那裏得到正確的答案,因爲我試過不同的方法來解決這個問題,但我發現TweetComposer.Builder
仍然我無法使用它。如何從應用程序的背景將媒體上傳到Twitter,而不會向用戶顯示除登錄屏幕之外的任何UI?
我試圖訪問推特developer website 但我沒有從那裏得到正確的答案,因爲我試過不同的方法來解決這個問題,但我發現TweetComposer.Builder
仍然我無法使用它。如何從應用程序的背景將媒體上傳到Twitter,而不會向用戶顯示除登錄屏幕之外的任何UI?
我已經找到了我的問題的解決方案我已經下載並集成了一個圖書館從here與此庫的幫助下,我能夠發佈多個媒體
第1步:
第一次登錄到Twitter通過根據指導
步驟2積分從該link庫:
之後,您將可以使用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);
}
我已經嘗試此代碼它爲我工作。