ファイルサイズを決定するには、次の方法があります。
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();
}
問題は、メソッド getSize() がダイアログ関数の外で呼び出されたときに正しいファイル サイズを返すにもかかわらず、uploadSize が常にゼロであることです。指定された文字列パスは正しいです。その理由は何ですか?
PS Send は私のクラス名です