2016-07-28 43 views
0

我在我的Android應用程序中第一次集成微博,我可以發佈鳴叫,我想分享圖像從應用程序,我有圖像的URL(這是存儲在AmazonS3服務器)。我想從我的Android應用程序共享此圖像..please任何人都可以提供的步驟來實現這一Android的twitter鳴叫與圖像

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說我使用的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,之後我們必須p將其與所有媒體ID的狀態一起作爲後置。這完全適合我。