2012-12-20 36 views
5

文件的大小,我有以下方法來確定文件大小:機器人不能從對話框

public static long getSize(String path) { 

    File file = new File(path); 

    if (file.exists()) { 
     long size = file.length(); 

     return size; 
    } else { 
     Log.e("zero size", "the file size is zero!"); 
     return 0; 

    } 

現在我想說明以下方法(代碼是不完整)的對話:

public void gimmeDialog(String path_to_file) { 

    final Dialog dialog = new Dialog(this); 
    dialog.setContentView(R.layout.dialog); 
    dialog.setTitle("Confirm"); 

    TextView text = (TextView) dialog.findViewById(R.id.txtUploadInfo); 

    Button dialogButtonOK = (Button) dialog.findViewById(R.id.btnDialogOK); 

    long uploadSize = Send.getSize(path_to_file)/1024/1024; 

    text.setText("You are about to upload " 
      + Long.toString(uploadSize) 
      + " MB. You must accept the terms of service before you can proceed"); 

    dialogButtonOK.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      dialog.dismiss(); 
     } 
    }); 

    dialog.show(); 
} 

問題是uploadSize始終爲零,雖然getSize()方法在對話框函數外調用時返回正確的文件大小。給定的字符串路徑是正確的。可能是什麼原因?

P.S.發送是我的課程名稱

+0

是你的文件更大然後1 MB? –

回答

5

你正在一個整數除法,如果numenator比分母越小結果爲0

嘗試用雙師:

double uploadSize = 1.0 * Send.getSize(path_to_file)/1024/1024; 
1

調用getSize()並在對話框函數內部可以訪問的對話框函數之外設置一個變量。或者將它作爲變量傳遞給函數。

這並不能真正解釋問題,但它解決了問題。

+0

只是你知道;在評論中提問並且爲實際答案保留答覆帖子。他們是有效的問題,只是在錯誤的形式問:) – dutt

+0

好的,謝謝你的提示。我認爲我在這裏提出的建議可以解決這個問題,所以我把它作爲答案。也許我應該改述一下?或者只是刪除並添加評論? – breadbin

+0

WTF我很蠢。當然,這是將文件大小傳遞給對話框的更好方法。它的工作,謝謝。 – Droidman