2012-05-17 18 views
7

Possible Duplicate:
Can we post image on twitter using twitter API in Android?Android twitter tweet z obrazem

Pracuję w aplikacji Android i chcę ćwierkać komunikat i zdjęcie na Twitterze. Jestem w stanie tylko tweets ćwierkanie na Twitterze przez kod:

String token = prefs.getString(OAuth.OAUTH_TOKEN, ""); 
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""); 

AccessToken a = new AccessToken(token, secret); 
Twitter twitter = new TwitterFactory().getInstance(); 
twitter.setOAuthConsumer(Constants.CONSUMER_KEY, 
     Constants.CONSUMER_SECRET); 
twitter.setOAuthAccessToken(a); 
try { 

**twitter.updateStatus("New tweet");** 
     twitter.//Which property of twitter should I use to tweet an image and //message 
} catch (TwitterException e) { 
    // TODO Auto-generated catch block 
    Log.e("Errorssssssssssssss", e.toString()); 
} 

Jak mogę dołączyć obraz, jak również?

+0

Skorzystaj z tego pytania [stackoverflow.com/questions/7609656/can-we-post-image-on-twitter-using-twitter-api-in-android](http://stackoverflow.com/questions/7609656/ can-we-post-image-on-twitter-using-twitter-api-in-android) – Aerrow

+0

spróbuj tego http://stackoverflow.com/questions/17093499/how-to-post-image-to-twitter-in -android/20633178 # 20633178 – Prachi

Odpowiedz

12

patrz http://www.londatiga.net/it/how-to-post-twitter-status-from-android/ użyć twitter4j bibliotekę

public void uploadPic(File file, String message) throws Exception { 
    try{ 
    StatusUpdate status = new StatusUpdate(message); 
    status.setMedia(file); 
    mTwitter.updateStatus(status);} 
    catch(TwitterException e){ 
     Log.d("TAG", "Pic Upload error" + e.getErrorMessage()); 
     throw e; 
    } 
} 

gdzie mTwitter jest instancją klasy Twitter

Upewnij się, że korzystasz z najnowszej wersji twitter4j-core pliku jar.

+0

status.setMedia (plik); Cześć. Nie jestem w stanie uzyskać własności o nazwie setMedia. Proszę, pomóż mi – Arun

+2

użyć twitter4j-core-2.2.5.jar, może używasz starej biblioteki –

+0

Cześć Gaurav Vashisth, mam właściwość setMedia w pliku jar twitter4j-core -2.2.5.jar, ale pojawia się błąd: Nie można znaleźć klasy "twitter4j.auth.AccessToken", odwołując się do metody android.twitter.TwitterUtils. To jest kod, którego użyłem do uwierzytelnienia AccessToken accesstoken = new AccessToken (token, secret); \t \t Twitter twitter = new TwitterFactory(). GetInstance(); \t \t twitter.setOAuthConsumer (Constants.CONSUMER_KEY, \t \t \t \t Constants.CONSUMER_SECRET); \t \t twitter.setOAuthAccessToken (dołączony); – Arun

2

U można próbować przykład, który przychodzi z kodem Twitter4j Library.Following u będzie pomóc

public final class TwitpicImageUpload { 
    /** 
    * Usage: java twitter4j.examples.media.TwitpicImageUpload [API key] [message] 
    * 
    * @param args message 
    */ 
    public static void main(String[] args) { 
     if (args.length < 2) { 
      System.out.println("Usage: java twitter4j.examples.media.TwitpicImageUpload [API key] [image file path] [message]"); 
      System.exit(-1); 
     } 
     try { 
      Configuration conf = new ConfigurationBuilder().setMediaProviderAPIKey(args[0]).build(); 
      ImageUpload upload = new ImageUploadFactory(conf).getInstance(MediaProvider.TWITPIC); 
      String url; 
      if (args.length >= 3) { 
       url = upload.upload(new File(args[1]), args[2]); 
      } else { 
       url = upload.upload(new File(args[1])); 
      } 
      System.out.println("Successfully uploaded image to Twitpic at " + url); 
      System.exit(0); 
     } catch (TwitterException te) { 
      te.printStackTrace(); 
      System.out.println("Failed to upload the image: " + te.getMessage()); 
      System.exit(-1); 
     } 
    } 
} 

Pobierz Twitter4j Biblioteka szukać więcej przykładów tam.

Powiązane problemy