0

我使用下面的代码将图像发布到 Twitter,

我在我的应用程序中使用 twitter4j-core-2.2.5 jar 文件,我真的不明白为什么它没有将图像发布到 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有什么问题还是我需要改变什么

请回答 谢谢

4

1 回答 1

0

试试这个:Android 演示。愿它有帮助。

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
    }
};
于 2013-02-26T09:46:55.843 回答