2013-02-26 72 views
-1

是否可以使用twitter api將圖片發佈至twitter?使用twitter api在android上傳圖片至twitter

如果可能該怎麼辦,請提供一些相關信息。

感謝

+0

可能重複[發佈圖片在Android的Twitter](http://stackoverflow.com/questions/9724142/post-pictures-to-twitter-in-android) – 2013-02-26 09:42:55

回答

0

試試這個:Android Demo。願它幫助。

if(isConnected()){ 
    OAuthService authService = new ServiceBuilder() 
           .provider(TwitterApi.class) 
           .apiKey(Constants.CONSUMER_KEY) 
           .apiSecret(Constants.CONSUMER_SECRET) 
           .callback(Constants.OAUTH_CALLBACK_URL) 
           .build(); 
    final OAuthRequest request = new OAuthRequest(Verb.POST, "https://upload.twitter.com/1/statuses/update_with_media.json"); 
       verifier = uri.getQueryParameter(Constants.IEXTRA_OAUTH_VERIFIER); 
       try { 
        Token token = getToken(); 
        if(token!=null){ 
         authService.signRequest(token, request); 
         MultipartEntity entity = new MultipartEntity(); 

         entity.addPart("status", new StringBody("Your Status!"));  // THIS IS THE TWITTER MESSAGE 
         entity.addPart("media", new FileBody(new File(uriString))); // THIS IS THE PHOTO TO UPLOAD 

         ByteArrayOutputStream out = new ByteArrayOutputStream(); 
         entity.writeTo(out); 

         request.addPayload(out.toByteArray()); 
         request.addHeader(entity.getContentType().getName(), entity.getContentType().getValue()); 
        } 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } catch(IOException e){ 
        e.printStackTrace(); 
       } 
       progressDialog = ProgressDialog.show(this,Constants.LABEL_BLANK, "Uploading Image",true); 
       Thread requestThread = new Thread() { 
        public void run() { 
         try { 
          response = new JSONObject (request.send().getBody()); 
          uiHandler.sendEmptyMessage(0); 
         }catch (JSONException e) { 
          Log.e("TOUR_APP_TAG", "JSONException Thrown: " + e.getMessage()); 
          errorHandler.sendEmptyMessage(0); 
         } 
        } 
       }; 
       requestThread.start(); 
} 
... 
... 
final Handler uiHandler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     if(progressDialog != null)progressDialog.dismiss(); 
     if(response.has("id")) 
      //your code 
    } 
}; 

final Handler errorHandler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     if(progressDialog != null)progressDialog.dismiss(); 
      //your code 
    } 
}; 
1

您需要的Twitter4J庫共享事先知情同意和這應該是最新的一個可從here

採取並且可以使用下面的方法來張貼圖片的

** 
* To upload a picture with some piece of text. 
* 
* 
* @param file The file which we want to share with our tweet 
* @param message Message to display with picture 
* @param twitter Instance of authorized Twitter class 
* @throws Exception exception if any 
*/ 

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