0

I am integrating twitter first time in my android application, i am able to post tweet,i want to share image from app,i have URL of images(Which is stored at AmazonS3 server).i want to share this image from my android app ..please anyone can provide steps to achieve this

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);
    }

}
Morgan
  • 4,143
  • 27
  • 35
Hardik Mehta
  • 867
  • 1
  • 12
  • 20

1 Answers1

1

first of all we have to download all the images as said by @amit i used 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;
        }
    }

then we have to upload it to twitter using rest api to get media id using status service and after thatwe have to post it along with status with all media id as post. this perfectly works for me.

Hardik Mehta
  • 867
  • 1
  • 12
  • 20