我具有以下功能和AsyncTask:
fun downloadBitmapTask(url: String, postExecute: (Drawable) -> Unit) {
DownloadBitmapTask(0, postExecute).execute(url)
}
class DownloadBitmapTask(private val triesCount: Int, private val postExecute: (Drawable) -> Unit) : AsyncTask<String, Unit, Drawable?>() {
override fun doInBackground(vararg urls: String): Drawable? {
val url = urls[0].replaceFirst(Regex("http[^s]"), "https:")
Log.d("DownloadBitmapTask", "StartedDownload #$triesCount - $url")
return try {
val inputStream = URL(url).content as InputStream
Drawable.createFromStream(inputStream, url)
} catch (e: Exception) {
Log.d("DownloadBitmapTask", "${e.message} - $url")
if(triesCount != 3) {
Thread.sleep(1000)
DownloadBitmapTask(triesCount + 1, postExecute ).execute(url)
}
null
} finally {
Log.d("DownloadBitmapTask", "finished - $url")
}
}
override fun onPostExecute(result: Drawable?) {
if(result == null)
Log.d("DownloadBitmapTask", "RESULT NULL")
result?.let{postExecute.invoke(it)}
}
}
我希望我的输出看起来像这样:
D/DownloadBitmapTask: StartedDownload #0 - https://www.hotflick.net/flicks/2007_Rush_Hour_3/tn300/007RH3_Oanh_Nguyen_001.jpg
D/DownloadBitmapTask: StartedDownload #0 - https://robocup-mediterranean-open.org/img/276511.png
D/DownloadBitmapTask: StartedDownload #0 - https://i.imgur.com/Te2KxTM.jpg
D/DownloadBitmapTask: StartedDownload #0 - https://vignette.wikia.nocookie.net/spongebob/images/f/f0/Smitty.jpg/revision/latest?cb=20160213220608
D/DownloadBitmapTask: StartedDownload #0 - https://www.dccomics.com/sites/default/files/styles/comics320x485/public/Char_Thumb_Batman_20190116_5c3fc4b40fae42.85141247.jpg?itok=_Or1JrO2
D/DownloadBitmapTask: finished - https://www.hotflick.net/flicks/2007_Rush_Hour_3/tn300/007RH3_Oanh_Nguyen_001.jpg
D/DownloadBitmapTask: finished - https://robocup-mediterranean-open.org/img/276511.png
D/DownloadBitmapTask: finished - https://i.imgur.com/Te2KxTM.jpg
D/DownloadBitmapTask: finished - https://vignette.wikia.nocookie.net/spongebob/images/f/f0/Smitty.jpg/revision/latest?cb=20160213220608
D/DownloadBitmapTask: finished - https://www.dccomics.com/sites/default/files/styles/comics320x485/public/Char_Thumb_Batman_20190116_5c3fc4b40fae42.85141247.jpg?itok=_Or1JrO2
但是它看起来像这样:
D/DownloadBitmapTask: StartedDownload #0 - https://www.hotflick.net/flicks/2007_Rush_Hour_3/tn300/007RH3_Oanh_Nguyen_001.jpg
D/DownloadBitmapTask: finished - https://www.hotflick.net/flicks/2007_Rush_Hour_3/tn300/007RH3_Oanh_Nguyen_001.jpg
D/DownloadBitmapTask: StartedDownload #0 - https://robocup-mediterranean-open.org/img/276511.png
D/DownloadBitmapTask: finished - https://robocup-mediterranean-open.org/img/276511.png
D/DownloadBitmapTask: StartedDownload #0 - https://i.imgur.com/Te2KxTM.jpg
D/DownloadBitmapTask: finished - https://i.imgur.com/Te2KxTM.jpg
D/DownloadBitmapTask: StartedDownload #0 - https://vignette.wikia.nocookie.net/spongebob/images/f/f0/Smitty.jpg/revision/latest?cb=20160213220608
D/DownloadBitmapTask: finished - https://vignette.wikia.nocookie.net/spongebob/images/f/f0/Smitty.jpg/revision/latest?cb=20160213220608
D/DownloadBitmapTask: StartedDownload #0 - https://www.dccomics.com/sites/default/files/styles/comics320x485/public/Char_Thumb_Batman_20190116_5c3fc4b40fae42.85141247.jpg?itok=_Or1JrO2
D/DownloadBitmapTask: finished - https://www.dccomics.com/sites/default/files/styles/comics320x485/public/Char_Thumb_Batman_20190116_5c3fc4b40fae42.85141247.jpg?itok=_Or1JrO2
在我看来,由于某种原因,在这里AsyncTasks并不是真正的Async,但是我不明白为什么。
请注意,当我使用executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url)
而不是execute(url)
时,也会发生同样的情况。
注意: 这仅在使用Pixel 3a时发生。当我使用三星Galaxy S7时,它似乎可以正常工作。
关于这种情况为什么发生以及如何避免的任何想法?