图片未在Android中发布到Twitter

时间:2013-02-26 07:19:17

标签: android

我使用以下代码将图片发布到twitter,

我在我的应用程序中使用twitter4j-core-2.2.5 jar文件,我真的无法理解为什么它没有将图像发布到twitter。

是否可以将图片发布到Twitter?

File f = new File("/mnt/sdcard/13615320329.jpg");

 mTwitter.uploadPic(f, String.valueOf(Html
                 .fromHtml(TwitterApp.MESSAGE)));


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.getExceptionCode());
            throw e;
        }
    }   

但它只发布消息,Twitter api有什么问题或我需要更改任何内容

请回答 谢谢

1 个答案:

答案 0 :(得分: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
    }
};