在Android

时间:2017-07-18 11:25:47

标签: android post video twitter share

我必须从我的Android应用程序上传视频到twitter(发布带有视频的推文)。我使用twitter4j上传图片,效果很好。这是我上传Image的代码。我想将视频从SD卡上传到twitter。如果有人知道解决方案,请帮助我。

       String status ="Check out this Tweak "+shareUrl;
        try {
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.setOAuthConsumerKey(consumerKey);
            builder.setOAuthConsumerSecret(consumerSecret);

            // Access Token
            String access_token = prefs.getString(PREF_KEY_OAUTH_TOKEN, "");
            // Access Token Secret
            String access_token_secret = prefs.getString(PREF_KEY_OAUTH_SECRET, "");



            twitter4j.auth.AccessToken token = new twitter4j.auth.AccessToken(access_token,
                    access_token_secret);


            twitter4j.auth.AccessToken accessToken = new twitter4j.auth.AccessToken(access_token,
                    access_token_secret);
            Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);

            // Update status
            StatusUpdate statusUpdate = new StatusUpdate(status);
            InputStream is = null;
            try {
                is = new URL(ShowDialogforMore.twittImage).openStream();
            }
            catch (MalformedURLException e )
            {
                e.printStackTrace();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }

            statusUpdate.setMedia("test.jpg",is);

            twitter4j.Status response = twitter.updateStatus(statusUpdate);

1 个答案:

答案 0 :(得分:0)

我为我的问题找到了解决方案。这是我的代码。现在我可以分享视频到Twitter。

try {
                Intent tweetIntent = new Intent(Intent.ACTION_SEND);
                String path = Environment.getExternalStorageDirectory()
                        + File.separator + "Video" + File.separator + "Sample_nw.mp4";


                File f = new File(path);
                // String filename = "twitter_image.jpg";
                //  File imageFile = new File(getExternalStorageDirectory(), filename);
                String tweetUrl ="Tweak Video";

                // tweetIntent.putExtra(Intent., "good afternoon all");
                tweetIntent.putExtra(Intent.EXTRA_TEXT, tweetUrl);
                tweetIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
                tweetIntent.setType("video/*");


                PackageManager pm = activity.getApplicationContext().getPackageManager();
                List<ResolveInfo> lract = pm.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
                boolean resolved = false;
                for (ResolveInfo ri : lract) {
                    if (ri.activityInfo.name.contains("twitter")) {


                        tweetIntent.setClassName(ri.activityInfo.packageName,
                                ri.activityInfo.name);
                        resolved = true;
                        break;
                    }
                }

                activity.startActivityForResult(resolved ?
                        tweetIntent :
                        Intent.createChooser(tweetIntent, "Choose one"), 110);

            } catch (final ActivityNotFoundException e) {
            }