magnusja / libaums

Open source library to access USB Mass Storage devices on Android without rooting your device
Apache License 2.0
1.26k stars 270 forks source link

Save jpg file successfully but it can not be opened when use BufferedOutputStream #391

Open evanyixie opened 1 year ago

evanyixie commented 1 year ago

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.