libaums
libaums copied to clipboard
Save jpg file successfully but it can not be opened when use BufferedOutputStream
Problem
The code below works well, and the jpg file can be open:
val inputStream = context.contentResolver.openInputStream(mediaFile.uri)
val outputStream = BufferedOutputStream(UsbFileOutputStream(file), chunkSize)
val buffer = ByteArray(chunkSize)
var len = 0
while (inputStream!!.read(buffer).also { len = it } != -1) {
outputStream.write(buffer, 0, len)
}
outputStream.flush()
outputStream.close()
inputStream.close()
But when I want to improve the performance, so I changed the buffer size of BufferedOutputStream to 10 * chunkSize:
val outputStream = BufferedOutputStream(UsbFileOutputStream(file), 10 * chunkSize)
The jpg file can not be opened, may be it had been broken.
Expected behavior
The jpg file saved in USB device should be opened.
Actual behavior
The jpg file saved in USB device can not be opened, maybe it had been broken
I don't know how to solve this problem, would you help me? Thank you very much.