我有以下方法来确定文件大小:
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() 方法在对话框函数外部调用时返回正确的文件大小。给定的字符串路径是正确的。可能是什么原因?
附言发送是我的类(class)名
最佳答案
您正在进行整数除法,如果分子小于分母,则结果为 0。
尝试双除法:
double uploadSize = 1.0 * Send.getSize(path_to_file) / 1024 / 1024;
关于android无法从对话框中获取文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13972147/