2016-07-28 10 views
0

私はアンドロイドアプリケーションでTwitterを初めて統合しています。私はtwitterを投稿できます。アプリからイメージを共有したい、イメージのURLを持っています(AmazonS3サーバーに保存されています).i want私のAndroidアプリからこの画像を共有することは、誰もがこのアンドロイドtwitter tweet with images

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

} 
+1

最初に画像をダウンロードし、twitterで画像を共有する –

+0

Rest Apiを使用していますか? @amit –

+0

あなたのコードにツイートのテキストを表示する – Vickyexpert

答えて

1

我々は@amitで言ったように、すべての画像をダウンロードする必要があり、すべての最初を達成するための手順を提供することができます..please私は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; 
     } 
    } 

を使用ステータスサービスを使用してメディアIDを取得するために残りのAPIを使用してtwitterにアップロードする必要があります。すべてのメディアidの状態を投稿として投稿してください。これは私にとって完璧に機能します。