1

我试图找到优雅的方法将方法OutputStream生成的转换bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream)为 Okio 的Source/ InputStream(需要进一步操作数据 - 编码),同时维护数据缓冲区。

我尝试使用

val pipe = Pipe(100)
bitmap.compress(Bitmap.CompressFormat.PNG, 100, Okio.buffer(pipe.sink()).outputStream())
        saveFile(File("filename"), pipe.source())

但这仍然存在bitmap.compress

4

1 回答 1

0

改用缓冲区http://square.github.io/okio/1.x/okio/okio/Buffer.html

val buffer = Buffer()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, buffer.outputStream())
saveFile(File("filename"), buffer)

Pipe 会假设一个并发的 writer 以避免潜在的阻塞

http://square.github.io/okio/1.x/okio/okio/Pipe.html

连接的源和接收器。接收器的输出是源的输入。通常每个都由自己的线程访问:生产者线程将数据写入接收器,消费者线程从源读取数据。此类使用缓冲区来解耦源和接收器。此缓冲区具有用户指定的最大大小。当生产者线程超过其消费者时,缓冲区会填满并最终写入接收器将阻塞,直到消费者赶上。

于 2019-01-23T17:30:24.573 回答