【发布时间】:2018-08-03 13:27:09
【问题描述】:
我正在使用AsyncTask 从 Facebook 加载照片。
主要问题是bitmapProf(类AsyncTaskLoadPhoto,方法-doInBackground)总是返回null。
我使用调试器和日志搜索错误,但没有找到错误。
这是我的代码:
private lateinit var imgProfile: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
.....
loadImg()
}
}
fun loadImg() {
var loadPhoto = AsyncTaskLoadPhoto()
var bitmap: Bitmap?
loadPhoto.execute()
bitmap = loadPhoto.get()
Log.i("TAG", bitmap.toString())
imgProfile.setImageBitmap(bitmap)
}
}
class AsyncTaskLoadPhoto : AsyncTask<Void, Void, Bitmap?>() {
override fun doInBackground(vararg p0: Void): Bitmap? {
var bitmapProf: Bitmap? = null
try {
val stringURL = "http://graph.facebook.com/"+"userID"+"/picture?width=150&width=150"
val imgURL = URL(stringURL)
val connection: HttpURLConnection = imgURL.openConnection() as HttpURLConnection
connection.doInput = true
connection.connect()
val inputStream = connection.inputStream
bitmapProf = BitmapFactory.decodeStream(inputStream)
} catch (e: Exception) {
e.printStackTrace()
}
return bitmapProf
}
【问题讨论】:
-
我不确定 kotlin 但对于 java 你不能再使用 asynctask 进行网络调用
标签: android android-asynctask kotlin inputstream httpurlconnection