【发布时间】:2021-03-19 20:59:15
【问题描述】:
我已经编写了这段代码 sn-p 来将图像文件从 Firebase 存储下载到本地存储。
contentResolver.openOutputStream(uri)?.use { ops -> // *
Firebase.storage.getReferenceFromUrl(model.mediaUrl).stream.await().stream.use { ips ->
val buffer = ByteArray(1024)
while (true) {
val bytes = ips.read(buffer) // *
if (bytes == -1)
break
ops.write(buffer, 0, bytes) // *
}
}
}
在标记的行中,android studio 给我Inappropriate blocking method call 警告,突出显示 openOutputStream()、read() 和 write() 函数。我已经运行了几次代码并且它运行正常。整个代码 sn-p 在一个挂起函数中,从 IO CoroutineScope 调用。
有人请告诉我此警告的实际原因和解决方案。
编辑此代码在以下上下文中调用。
fun someFunc() {
lifecycleScope.launch(IO) {
val uri = getUri(model)
...
}
...
}
...
suspend fun getUri(model: Message): Uri {
... // Retrive the image file using mediastore.
if ( imageNotFound ) return downloadImage(model)
else return foundUri
}
suspend fun downloadImage(model: Message): Uri {
... // Create contentvalues for new image.
val uri = contentResolver.insert(collectionUri, values)!!
// Above mentioned code snippet is here.
return uri
}
【问题讨论】:
-
请显示此代码的更多上下文,以便我们了解它是如何在 IO 调度程序上调用的。顺便说一句,没有理由在
use的目标上调用close()。 -
@Tenfour04 更多上下文意味着什么?您是否希望我编辑帖子以添加调用此函数的代码。
-
是的,围绕此代码或调用此代码的代码。或者整个挂起函数。
-
@Tenfour04 刚刚添加了初始代码的上下文。现在看看有没有帮助。
标签: android kotlin inputstream kotlin-coroutines outputstream