Sim*_*one 4 android android-context kotlin kotlin-coroutines
在我的服务中,我需要调用onStartCommand一些需要的方法,withContext(Dispatchers.IO)例如CoroutineScope(Dispatchers.IO):
但Suspend function 'withContext' should be called only from a coroutine or another suspend function。所以 ifonStartCommand不能是一个挂起函数,因为它具有重写,并且由于方法withContext而不能被 CoroutineScope 调用Inappropriate blocking method call
val url = URL(pokemon.linkImage)
val iS = url.openConnection().getInputStream()
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我的onStartCommand():
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
//create the directory on internal storage that will contains all pokemon images
val path = applicationContext.filesDir.absolutePath
val dirName = "/pokeimg"
val dir = File(path, dirName)
if(!dir.exists())
dir.mkdir()
CoroutineScope(Dispatchers.IO).launch {
//get the list of pokemon when they are loaded from repository
val listOfPokemon =
PersistenceSingletonRepository.getInstance(applicationContext).getListOfPokemon()
//download all the image for each pokemon in the list, but only if the image is
//not present in the directory
for(pokemon in listOfPokemon) {
val imageName = "${pokemon.name}.png"
val file = File("${dir.absolutePath}/$imageName")
//check if image not exists on internal storage and if is not an official-artwork
//images that aren't official-artwork has less quality so don't resize
if(!file.exists()) {
//download img
val url = URL(pokemon.linkImage)
val iS = url.openConnection().getInputStream()
val opts = BitmapFactory.Options()
if (!Utils.isBadImage(pokemon.linkImage)) {
//request a smaller image
opts.inSampleSize = 2
}
val bitmap = BitmapFactory.decodeStream(iS, null, opts)
val fOut= FileOutputStream(file)
bitmap?.compress(Bitmap.CompressFormat.PNG, 100, fOut)
fOut.flush()
fOut.close()
}
}
stopSelf()
}
return START_NOT_STICKY
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3924 次 |
| 最近记录: |