问题
I want to share image url and text on twitter wall. And image should display on twitter wall.Please help me how can i achieve this functionality? And how to achieve this solution using share intent?
回答1:
You can try this: Share image and text
final String surl="https://i.stack.imgur.com/zlR4C.jpg";
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap _bitmap, Picasso.LoadedFrom from) {
Bitmap bmp = _bitmap;
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bmp, "SomeText", null);
Log.d("Path", path);
Uri screenshotUri = Uri.parse(path);
TweetComposer.Builder builder = null;
try {
builder = new TweetComposer.Builder(context)
.text("Hi Twittew")
.image(screenshotUri);
} catch (MalformedURLException e) {
e.printStackTrace();
}
builder.show();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso.with(context).load(surl).into(target);
回答2:
I would highly recommend the following library to achieve this: https://code.google.com/p/socialauth-android/
Also, here's a little guide on how to install and use the library:
http://www.3pillarglobal.com/insights/part-3-using-socialauth-to-integrate-twitter-api-in-android
回答3:
App Setup
Create App here https://developer.twitter.com/en/apps/create
Add Callback URLs to twittersdk:// (For Android SDK)
From App Details goto Keys and tokens and add in res/values/strings.xml
<string name="twitter_api_key">REPLACE_KEY</string>
<string name="twitter_api_secret">REPLACE_SECRET</string>
From App Details goto Permissions -> Edit
Access permission -> Read, write, and Direct Messages
Additional permissions -> Check to true (Request email address from users)
Save
Add INTERNET permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Add twitter SDK dependency to build.gradle (Module:app)
dependencies {
implementation 'com.twitter.sdk.android:twitter:3.1.1'
//implementation 'com.twitter.sdk.android:twitter-core:3.1.1'
//implementation 'com.twitter.sdk.android:tweet-ui:3.1.1'
}
In Activity
private TwitterAuthClient twitterAuthClient;
Custom Button Click
TwitterConfig config = new TwitterConfig.Builder(this)
.logger(new DefaultLogger(Log.DEBUG))
.twitterAuthConfig(new TwitterAuthConfig(getResources().getString(R.string.twitter_api_key), getResources().getString(R.string.twitter_api_secret)))
.debug(true)
.build();
Twitter.initialize(config);
twitterAuthClient = new TwitterAuthClient();
TwitterSession twitterSession = TwitterCore.getInstance().getSessionManager().getActiveSession();
if (twitterSession == null) {
twitterAuthClient.authorize(this, new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
TwitterSession twitterSession = result.data;
shareOnTwitter();
}
@Override
public void failure(TwitterException e) {
Log.e("Twitter", "Failed to authenticate user " + e.getMessage());
}
});
} else {
shareOnTwitter();
}
private void shareOnTwitter() {
StatusesService statusesService = TwitterCore.getInstance().getApiClient().getStatusesService();
Call<Tweet> tweetCall = statusesService.update("First tweet from Android", null, false, null, null, null, false, false, null);
tweetCall.enqueue(new Callback<Tweet>() {
@Override
public void success(Result<Tweet> result) {
Log.e("Twitter", "Twitter Share Success");
logoutTwitter();
}
@Override
public void failure(TwitterException exception) {
Log.e("Twitter", "Twitter Share Failed with Error: " + exception.getLocalizedMessage());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (twitterAuthClient != null) {
twitterAuthClient.onActivityResult(requestCode, resultCode, data);
}
}
public void logoutTwitter() {
TwitterSession twitterSession = TwitterCore.getInstance().getSessionManager().getActiveSession();
TwitterCore.getInstance().getSessionManager().clearActiveSession();
if (twitterSession != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();
} else {
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
cookieSyncMngr.startSync();
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncMngr.stopSync();
cookieSyncMngr.sync();
}
}
}
来源:https://stackoverflow.com/questions/27705036/android-twitter-sharing-with-image-url-and-text