2017-07-18 59 views
0

我必須從我的Android應用程序上傳視頻到Twitter(發佈視頻鳴叫)。我使用twitter4j上傳圖片,效果很好。這是我上傳Image.I的代碼,我想將視頻從sd卡上傳到twitter。如果有人知道解決方案,請幫助我。分享視頻從SD卡到微博在Android

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

檢查此https://stackoverflow.com/questions/20681924/upload-video-on-twitter – Ninja

回答

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) { 
      }