I'm using this code
for (Uri fileUri : files) {
File destFile = ...
InputStream is = getContentResolver().openInputStream(fileUri);
FileUtils.copyInputStreamToFile(is, destFile);
}
And if user remove the battery of the phone after this for
cycle then the some files (lasts) are corrupted.
Is it possible to prevent this?
I tried copy the InputStream to the temp directory and then use
FileUtils.moveFile(temp, destFile);
for each file but there was the same problem.
Can I somehow close the copied files? Make sure they are copied successfully?
I solved this issue by using this:
FileOutputStream out = new FileOutputStream(filename);
[...]
out.flush();
out.getFD().sync();
from I/O concept flush vs sync
Look at the https://android.googlesource.com/platform/frameworks/base/+/cd92588/core/java/android/os/FileUtils.java and method copyToFile(InputStream inputStream, File destFile)
It's nice example how to sole this.
Thanks to qbix!