在Android中将图像和数据共享到Twitter

时间:2014-05-06 06:34:01

标签: android twitter

我正在开发一个应用程序,我必须从我的Android应用程序共享数据和图像到Twitter。我正在关注http://chintankhetiya.wordpress.com/2013/07/18/sharing-text-image-in-twitter-android-example/comment-page-1/#comment-285这个。我确实更改了消费者密钥和消费者秘密。而运行我的应用程序一个Toast消息即将到来登录Faild。请告诉我这个需要的任何东西

2 个答案:

答案 0 :(得分:1)

对于Twitter分享,如果是安装,您可以直接从Twitter应用程序共享文件,如果应用程序未安装,您可以使用twitter api。

下面是两件事的代码

Boolean isTwitterAppAvailable =false;
try {

            Intent shareIntent = new Intent(
                    android.content.Intent.ACTION_SEND);

            shareIntent.setType("image/*");
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                    "Content to share");

            PackageManager pm = getPackageManager();
            List<ResolveInfo> activityList = pm.queryIntentActivities(
                    shareIntent, 0);
            for (final ResolveInfo app : activityList) {
                if (app.activityInfo.name.contains("twitter")) {

                    final ActivityInfo activity = app.activityInfo;
                    final ComponentName name = new ComponentName(
                            activity.applicationInfo.packageName,
                            activity.name);
                    shareIntent.setClassName("com.twitter.android",
                            "com.twitter.android.PostActivity");

                    shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    shareIntent.setComponent(name);

                    shareIntent.putExtra(Intent.EXTRA_TEXT,
                            GlobalApplication.twitter_share_msg);

                    if (GlobalApplication.isImageShareTwitter)
                        shareIntent.putExtra(Intent.EXTRA_STREAM,
                                Uri.parse("file:///sdcard/attachment.jpg"));
                    isTwitterAppAvailable = true;

                    startActivity(shareIntent);

                    break;
                }
            }

            if (!isTwitterAppAvailable) {

                if (isNetworkAvailable()) {

                    GlobalApplication.casted_image = casted_image;

                    // twitter web sharing using twitter4j api...

                    Twitt_Sharing twitt = new Twitt_Sharing(
                            ShowCampaignDetail.this, twitter_consumer_key,
                            twitter_secret_key);

                    twitt.shareToTwitter(
                            GlobalApplication.twitter_share_msg,
                            casted_image);

                    Twitter_Handler tHandler = new Twitter_Handler(
                            ShowCampaignDetail.this, twitter_consumer_key,
                            twitter_secret_key);
                    User user;
                    try {
                        user = tHandler.twitterObj
                                .showUser(tHandler.twitterObj.getId());
                        String url = user.getProfileImageURL();
                    } catch (IllegalStateException e) { // TODO
                                                        // Auto-generated
                        e.printStackTrace();
                    } catch (TwitterException e) {
                        e.printStackTrace();
                    }

                } else {
                    Toast.makeText(ShowCampaignDetail.this,
                            "No Network Connection Available !!!",
                            Toast.LENGTH_LONG).show();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

答案 1 :(得分:-1)

Use Following code :

/*
 * Share any Image.
 */
private void shareImage() {
    Intent share = new Intent(Intent.ACTION_SEND);
    // If you want to share a png image only, you can do:
    // setType("image/png"); OR for jpeg: setType("image/jpeg");
    share.setType("image/*");
    // Make sure you put example png image named myImage.png in your
    // directory
    String imagePath = Environment.getExternalStorageDirectory()
            + "/myImage.png";
    File imageFileToShare = new File(imagePath);
    Uri uri = Uri.fromFile(imageFileToShare);
    share.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(share, "Share image to..."));
}