2012-06-26 23 views
0

我有一個應用程序,我正在上傳一張圖片到Facebook相冊。該應用上傳照片很好,沒有問題。如果我硬編碼,我也可以在照片上加上標題。我想要做的是製作一個警報對話框,捕獲用戶標題,然後在上傳圖片之前將其放入捆綁包中。發生了什麼是照片上傳,然後我得到對話框輸入標題。簡單的警報對話框問題 - Android

這裏是彈出警告對話框的方法...

public String createAlert() {  
     AlertDialog.Builder alert = new AlertDialog.Builder(this);     
      alert.setTitle("Enter Caption for Photo"); 
      alert.setMessage("Caption :"); 
      final EditText input = new EditText(this); 
      alert.setView(input); 

      alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        imageCaption = input.getText().toString(); 
        return;     
        } 
       }); 

       alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
         return; 
        } 
       }); 
       AlertDialog helpDialog = alert.create(); 
       helpDialog.show(); 
       return imageCaption; 

    } 

現在這裏是束並上傳到Facebook ...

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data);   
     switch (requestCode) { 
     case PICK_EXISTING_PHOTO_RESULT_CODE: { 

     if (resultCode == RESULT_OK){ 
       Uri photoUri = data.getData(); 
       String imagePath = getPath(photoUri); 
       byte[] data1 = null; 


       Bitmap bi = BitmapFactory.decodeFile(imagePath); 
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       bi.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
       data1 = baos.toByteArray(); 

       Bundle params = new Bundle(); 
       params.putString(Facebook.TOKEN, facebook.getAccessToken()); 
       params.putString("caption", createAlert()); 
       params.putByteArray("photo", data1); 

       try { 
        facebook.request("me/photos",params,"POST"); 
       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       } 


     break; 
     } 
     default: { 
     facebook.authorizeCallback(requestCode, resultCode, data); 
     break; 
     } 


    } 

    } 
+0

如果你需要看到完整的代碼,它是:https://github.com/adamwhiles/EasyPhotoUpload – adamwhiles

回答

0

在你createAlert方法完成的時間,用戶還沒有輸入任何內容。一旦您的代碼停止運行,將顯示對話框的事件剛添加到將來要處理的系統消息中。你的代碼然後繼續做Facebook的帖子。然後顯示對話框。然後,在點擊正確的事情時,OnClickListener中的代碼就會運行。

您需要在對話動作發生後發送Facebook帖子。我想直接做這件事的方法是將createAlert方法內聯到你現在調用它的那一行的上方。然後將其餘的Facebook代碼粘貼到onClick上。然後這會給你正確的順序,你可以再次分解成方法。

只是意識到調用show不會立即執行任何操作,並且onclick中的代碼也不會立即運行。這只是排隊一個事件,並指定分別發生另一個事件時會發生什麼。這是基於事件的編程,就像許多GUI API一樣。

編輯:我會嘗試編輯您的代碼以顯示如何使其工作。但是,您沒有足夠的時間進行編譯,因此可能需要進行一些清理。在這裏:

protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
     case PICK_EXISTING_PHOTO_RESULT_CODE: 

      if (RESULT_OK == resultCode) { 
       final Uri photoUri = data.getData(); 
       createAlert(photoUri); 
      } 

      break; 
     default: 
      facebook.authorizeCallback(requestCode, resultCode, data); 
      break; 
    } 
} 

public void createAlert(final Uri photoUri) { 
    final AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    alert.setTitle("Enter Caption for Photo"); 
    alert.setMessage("Caption :"); 

    final EditText input = new EditText(this); 
    alert.setView(input); 

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(final DialogInterface dialog, final int whichButton) { 
      final String caption = input.getText().toString(); 
      postToFacebook(photoUri, caption); 
     } 
    }); 

    alert.setNegativeButton("Cancel", null); 
    final AlertDialog helpDialog = alert.create(); 
    helpDialog.show(); 
} 

private void postToFacebook(final Uri photoUri, final String caption) { 
    final String imagePath = getPath(photoUri); 
    final byte[] data1 = null; 

    Bitmap bi = BitmapFactory.decodeFile(imagePath); 
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bi.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    data1 = baos.toByteArray(); 
    bi.recycle(); 
    bi = null; 

    final Bundle params = new Bundle(); 
    params.putString(Facebook.TOKEN, facebook.getAccessToken()); 
    params.putString("caption", caption); 
    params.putByteArray("photo", data1); 

    try { 
     facebook.request("me/photos", params, "POST"); 
    } catch (final FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (final MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (final IOException e) { 
     e.printStackTrace(); 
    } 
} 
+0

我不知道我理解你說究竟 – adamwhiles

+0

我會盡力編輯代碼以顯示如何使其工作。但是,您沒有足夠的時間進行編譯,因此可能需要進行一些清理。查看添加到上面的答案的新部分。 –

+0

完美工作,謝謝! – adamwhiles