Android 带有图片的 twitter 推文

Android twitter tweet with images

我是第一次在我的 android 应用程序中集成 Twitter,我可以 post 发推文,我想从应用程序分享图片,我有 URL 张图片(其中存储在 AmazonS3 服务器上)。我想从我的 android 应用程序中分享这张图片。请任何人提供实现此目的的步骤

public class TwitterIntegration extends GlobalActivity {
    TwitterAuthClient mTwitterAuthClient;
    TwitterApiClient twitterApiClient;
    Preferences preferences;
    UserHistory userHistory;
    StatusesService statusesService;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        preferences=HWUtil.getPreferences(this);
        userHistory=preferences.getUserHistory();
        mTwitterAuthClient=new TwitterAuthClient();
        mTwitterAuthClient.authorize(this, new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                TwitterSession session = result.data;
                Log.d("user", session.getUserName());
                Log.d("user", session.toString());
                HWUtil.showToast(TwitterIntegration.this, session.getUserName());
                twitterApiClient = TwitterCore.getInstance().getApiClient(session);
                statusesService = twitterApiClient.getStatusesService();
                statusesService.update("Hii from android", null, null, null, null,
                        null, null, null, new Callback<Tweet>() {
                            @Override
                            public void success(Result<Tweet> result) {
                                HWUtil.showToast(TwitterIntegration.this, "Posted SucessFully");
                                if(Validator.isNotNull(userHistory.getHistoryPictures())&& userHistory.getHistoryPictures().length>0){
                                    shareImage();
                                }



                            }

                            public void failure(TwitterException exception) {
                                HWUtil.showToast(TwitterIntegration.this, "Failed to post");
                            }
                        });
            }

            @Override
            public void failure(TwitterException exception) {
                HWUtil.showToast(TwitterIntegration.this, exception.getMessage());
            }
        });


    }

    private void shareImage() {
        if(Validator.isNotNull(twitterApiClient)){
            MediaService mediaService=twitterApiClient.getMediaService();

        }
    }


    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        // Pass the activity result to the login button.
        super.onActivityResult(requestCode,responseCode,intent);
        mTwitterAuthClient.onActivityResult(requestCode, responseCode, intent);
    }

}

首先,我们必须按照@amit 所说的那样下载所有图像,我使用了 asynctask

public class DownLoadImageAsyncTask extends AsyncTask{

        @Override
        protected void onPreExecute() {
            progressDialog=new ProgressDialog(TwitterIntegration.this);
            progressDialog.setCancelable(false);
            progressDialog.setMessage(getString(R.string.please_wait));
            progressDialog.setIndeterminate(true);
            if(Validator.isNotNull(preferences.getImagePath())&& !preferences.getImagePath().isEmpty()){
                preferences.getImagePath().clear();
            }
            filePath=preferences.getImagePath();
        }

        @Override
        protected Object doInBackground(Object[] params) {

            File file=new File(Environment.getExternalStorageDirectory(),"/HealthWel");
            if(file.exists()==true){
                file.delete();
            }
            file.mkdir();
            for (int i=0;i<mURLs.size();i++){
                File f=new File(file+"/"+i+".jpg");
                if(f.exists()==true){
                    f.delete();
                }
                if(f.exists()==false){
                    HttpClient httpClient=new DefaultHttpClient();
                    HttpGet httpGet=new HttpGet(mURLs.get(i));
                    try {
                        HttpResponse httpResponse=httpClient.execute(httpGet);
                        if(httpResponse.getStatusLine().getStatusCode()==200){
                            HttpEntity httpEntity=httpResponse.getEntity();
                            InputStream is=httpEntity.getContent();
                            Boolean status=f.createNewFile();
                            FileOutputStream fileOutputStream=new FileOutputStream(f);
                            byte[]buffer=new byte[1024];
                            long total=0;
                            int count;
                            while ((count=is.read(buffer))!=-1){
                                total+=count;
                                fileOutputStream.write(buffer,0,count);
                            }
                            if(!downLoad) {
                                if (Validator.isNotNull(preferences.getImagePath()) && !preferences.getImagePath().isEmpty()) {
                                    preferences.getImagePath().clear();
                                }
                            }

                            filePath.add(f.getPath());
                            fileOutputStream.close();
                            is.close();
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    // runs on UI thread
                                    progressDialog.show();
                                }
                            });

                        }
                        else {
                            finish();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            preferences.setImagePath(filePath);
            dismissProgressDialog();
            shareImage();
        }
    }

    private void showProgressDialog() {
        if(!isFinishing() && progressDialog==null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setCancelable(false);
            progressDialog.show();

        }

    }

    /**
     * dismiss Progress Dialog.
     */
    private void dismissProgressDialog() {
        if (!isFinishing() &&progressDialog!=null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog=null;
        }
    }

然后我们必须使用 rest api 将其上传到 Twitter 以使用状态服务获取媒体 ID,之后我们必须 post 它连同所有媒体 ID 的状态 post.这非常适合我。