Android Twitter与图片网址和文字共享

时间:2014-12-30 11:55:37

标签: android android-twitter

我想在twitter墙上分享图片网址和文字。图像应显示在twitter墙上。请帮助我如何实现此功能?以及如何使用共享意图实现此解决方案?

3 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:共享图像和文本

    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);

答案 1 :(得分:0)

我强烈建议使用以下库来实现此目的: https://code.google.com/p/socialauth-android/

此外,这里有一些关于如何安装和使用该库的指南:

http://www.3pillarglobal.com/insights/part-3-using-socialauth-to-integrate-twitter-api-in-android

答案 2 :(得分:0)

应用设置

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

在AndroidManifest.xml中添加INTERNET权限

<uses-permission android:name="android.permission.INTERNET" />

将twitter SDK依赖项添加到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'
}

活动中

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();
            }
        }
    }