4

On Samsung Galaxy S5 with latest Lineage OS nightly 9-Aug-2017 (Android 7.1.2):

I use FileOutputStream to write to a file on the app cache dir on the SDCard and close it. SD Card is not adopted.

File file = new File(fileName);
FileOutputStream fout = new FileOutputStream(file, true); // yes, append if exists
fout.write(buf, 0, len); // wrote a few MBs here
fout.close();

Later, when I list the dir, File.length() is 0 (zero) instead of a few MB. I can, however, read all the bytes in the file. But size still shows as zero.

File dir = new File(dirName);
File file = dir.listFiles()[index];
long fileSize = file.length(); // fileSize is zero until I reboot

Other apps such as Total Commander also see zero size, but also can copy it to a non-zero file.

After phone reboot, file shows correct size.

The factory installed file browser, "Files" shows the correct size always.

Also I tried on emulators Android version 6.0, 7.0, 7.1, 8.0 but all worked and size was correct.

Also this doesn't happen on the internal memory (or emulated storage). Only happens on SDCard.

Did not see any errors, exceptions while reading or writing etc.

RumburaK
  • 1,985
  • 1
  • 21
  • 30
  • Please provide a [mcve] showing how you are creating and writing the file. This feels like you are asking the `MediaStore` to index the file before you have written everything to the file. – CommonsWare Aug 15 '17 at 13:43
  • Thanks @CommonsWare I added code example. Please also note that I ask for size() even hours late, even from other apps and is still zero. Until reboot or eject! – RumburaK Aug 15 '17 at 14:45
  • 1
    I am assuming that `fut.close()` is supposed to be `fout.close()`. If so, add `fout.flush()` and `fout.getFD().sync()` after `fout.write()` and before `fout.close()`, and see if that helps. This is not a `MediaStore` problem (at least not completely) as I originally thought. It might be a bug in LineageOS. – CommonsWare Aug 15 '17 at 14:49
  • Thanks @CommonsWare, `FileOutputStream.flush()` is a NOOP, but the `getFD().sync()` might make a big difference. I will report back tonight when I get to try it. – RumburaK Aug 15 '17 at 14:59
  • Sadly I tried `flush()` and `getFD().sync()` but no change! – RumburaK Aug 15 '17 at 17:59
  • Funny, it happens on a 64GB Toshiba Exceria card but not an older SanDisk 8GB card and didn't happen! – RumburaK Aug 15 '17 at 18:24
  • 1
    IMHO, that points the finger even more to LineageOS. – CommonsWare Aug 15 '17 at 18:34
  • Indeed, after more tests I found out that LineageOS and exFat don't get along too well. Any other combinations work. I have added some details to: https://jira.lineageos.org/projects/BUGBASH/issues/BUGBASH-814 – RumburaK Aug 15 '17 at 20:38
  • @RumburaK i am facing the same problem did you find any solution? – Mateen Chaudhry Feb 16 '19 at 09:14
  • Try opening separately with append if it exists and no append if doesn't... – RumburaK Feb 20 '19 at 21:57

0 Answers0