Skip to content

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

@evanyixie

Description

@evanyixie

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions