我有以下方法来确定文件大小:
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;
}
Run Code Online (Sandbox Code Playgroud)
现在我想显示一个包含以下方法的对话框(代码未完成):
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();
}
Run Code Online (Sandbox Code Playgroud)
问题是uploadSize始终为零,但方法getSize()在对话框函数外部调用时返回正确的文件大小.给定的字符串路径是正确的.可能是什么原因?
PS发送是我的班级名称
您正在进行整数除法,如果numenator小于分母,则结果为0.
尝试双重划分:
double uploadSize = 1.0 * Send.getSize(path_to_file) / 1024 / 1024;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
162 次 |
| 最近记录: |