how do I get file size of temp file in android?

前端 未结 3 1132
一整个雨季
一整个雨季 2020-12-02 17:55

if I use openFileOutput() to create and write to a temp file how do I get filesize after I\'m done writing to it?

3条回答
  •  自闭症患者
    2020-12-02 18:32

    Try to use below code:

    // Get file from file name
        final String dirPath = f.getAbsolutePath();
        String fileName = url.substring(url.lastIndexOf('/') + 1);
        File file = new File(dirPath + "/" + fileName);
    
          // Get length of file in bytes
              long fileSizeInBytes = file.length();
         // Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
              long fileSizeInKB = fileSizeInBytes / 1024;
        //  Convert the KB to MegaBytes (1 MB = 1024 KBytes)
              long fileSizeInMB = fileSizeInKB / 1024;
    
              if (fileSizeInMB > 27) {
              ...
              }
    

    Hope It will work for you..!!

提交回复
热议问题