Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Suppress("Deprecation")
- class LoadPhotosAsyncTask(
- private val imageView: ImageView,
- private val src: String) : AsyncTask<Unit, Unit, Bitmap>() {
- private val imageViewReference = WeakReference<ImageView>(imageView);
- private val TAG = "LoadPhotosAsyncTask"
- override fun doInBackground(vararg params: Unit?): Bitmap? {
- Log.d(TAG, "loadPhoto: Started")
- Log.d(TAG, src)
- val url: URL = URL(src)
- //val url: URL = URL("https://cdn.wallpapersafari.com/65/31/aAzTvc.jpg")
- var connection: HttpURLConnection? = null
- var inputStream: InputStream? = null
- var bitmap: Bitmap? = null
- try {
- connection = url.openConnection() as HttpURLConnection
- connection.doInput
- connection.connect()
- inputStream = connection.inputStream
- val options = BitmapFactory.Options()
- options.inPreferredConfig = Bitmap.Config.RGB_565
- bitmap = BitmapFactory.decodeStream(inputStream, null, options)
- } catch (e: Exception) {
- Log.d(TAG, "${e.cause} ${e.message}")
- } finally {
- connection?.disconnect()
- inputStream?.close()
- }
- return bitmap
- }
- override fun onPostExecute(bitmap: Bitmap?) {
- if (bitmap != null) {
- val imageView: ImageView? = imageViewReference.get();
- imageView?.setImageBitmap(bitmap)
- } else {
- Log.d(TAG, "I hate all this shit")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment