android无法从对话框中获取文件大小

标签 android dialog

我有以下方法来确定文件大小:

    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/

相关文章:

java - 如何提高android中输入命令的速度?

java - 如何在点击 Android 应用程序中的任何按钮时禁用音频?

java - 单击按钮时不显示对话框 - Android

python - 如何关闭组合框事件更改时的 GTK 对话?

android - 如何创建一个 ListPreference 对话框以在非偏好 Activity 中显示

安卓 : How can I cache the tweets that I’ve got using the Fabric SDK for offline mode?

android - 未更新的应用程序执行的代码需要用户在 Android 6.0 Marshmallow 中不允许的权限时会发生什么

android - 将从服务器接收到的时间(UTC 时间)转换为本地时间

java - 如何创建一个自定义alertDialog,其中的列表绑定(bind)到onClick事件android?

android - 自定义对话框等待响应