【问题标题】:Retrieve File Size in Android programmatically以编程方式检索 Android 中的文件大小
【发布时间】:2018-05-08 02:45:16
【问题描述】:

我正在寻找一种读取“PDF”大小的方法。到目前为止,我尝试了以下方法:

注意:所有权限都已到位,我确实可以访问该文件。

1) 使用文件长度

path = "/storage/emulated/0/Download/c4611_sample_explain.pdf";
File file = new File(path);
int file_size = Integer.parseInt(String.valueOf(file.length() / 1024));

结果: 它适用于图像,但在 pdf 文件上返回 0

2) 使用光标

Uri uri = Uri.fromFile(new File(path));
Cursor cursor = contentResolver.query(uri, null, null, null, null);
String size= "null";
if (cursor != null) {
    size=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE));
    cursor.close();
}

结果:

size: null 
path: /storage/emulated/0/Download/c4611_sample_explain.pdf 
URI: file:///storage/emulated/0/Download/c4611_sample_explain.pdf

UPDATE: Cursor returns null

还有什么我应该做的吗?

【问题讨论】:

  • 在从cursor 读取值之前,您没有调用cursor.moveToFirst();
  • @Sagar 我怀疑这更多是空光标问题。以我使用 URI 的方式使用它有什么问题吗? file:// 如果是这样我怎么能得到content://
  • 您可以调试并检查它是否为空。我认为URI很好。在我们深入研究 URI 之前,请确保现有代码正常工作
  • 是的,我已经检查了 contentResolver.query(uri, null, null, null, null) 返回 null
  • Integer.parseInt(String.valueOf(file.length() / ??很奇怪,您将整数转换为字符串。请重试。

标签: android file uri


【解决方案1】:

在您发布的“使用光标”结果中,您使用的方案错误:

URI: file:///storage/emulated/0/Download/c4611_sample_explain.pdf

如果您看到 URI 以“file:”开头但使用游标方法,则 URI 必须以“content:”开头

首先你需要检查scheme,如果是“文件”或“内容”,那么你会得到文件大小。我希望这段代码有帮助:

if (resultCode == RESULT_OK && data != null) {
    Uri filePath = data.getData();
    if (data.getData().getScheme().equals("file")) {
        File file = new File(filePath.toString());
        int fileSize = (int) file.length();
        System.out.println("This is the file size: " + fileSize);
    } else if (data.getData().getScheme().equals("content")) {
        Cursor returnCursor = this.getContentResolver().query(filePath, null, null, null, null);
        assert returnCursor != null;
        int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
        returnCursor.moveToFirst();
        String fileSize = returnCursor.getString(sizeIndex);
        System.out.println("This is the file size: " + fileSize);
    }
}

【讨论】:

    【解决方案2】:

    将 if 语句更改为:

    if (cursor != null && cursor.moveToFirst() )
    

    并在您的查询方法中添加一个投影:

    String[] projection = { MediaStore.Files.FileColumns.SIZE };
    Cursor cursor = contentResolver.query(uri, projection, null, null, null);
    

    【讨论】:

    • 问题是。游标本身返回 null
    【解决方案3】:

    试试下面的方法

    格式化文件大小的方法

    public static String formatSize(long size) {
        String suffix = null;
    
        if (size >= 1024) {
            suffix = " Bytes";
            size /= 1024;
            if (size >= 1024) {
                suffix = " MB";
                size /= 1024;
            }
        }
        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
    
        int commaOffset = resultBuffer.length() - 3;
        while (commaOffset > 0) {
            resultBuffer.insert(commaOffset, ',');
            commaOffset -= 3;
        }
        if (suffix != null) resultBuffer.append(suffix);
        return resultBuffer.toString();
    }
    

    并从您的存储中读取文件

     String pathname= Environment.getExternalStorageDirectory().getAbsolutePath();
     String fullpath= pathname + "/dummy2.pdf";
     File file=new File(fullpath);
     String size = formatSize(file.length());// this will give you the size of the file
    

    希望对你有帮助。

    快乐编码 ;)

    【讨论】:

      猜你喜欢
      • 2011-10-13
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      相关资源
      最近更新 更多