Guest User

хелп

a guest
Jan 24th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.54 KB | None | 0 0
  1.  
  2. @Suppress("Deprecation")
  3. class LoadPhotosAsyncTask(
  4.         private val imageView: ImageView,
  5.         private val src: String) : AsyncTask<Unit, Unit, Bitmap>() {
  6.  
  7.     private val imageViewReference = WeakReference<ImageView>(imageView);
  8.     private val TAG = "LoadPhotosAsyncTask"
  9.  
  10.     override fun doInBackground(vararg params: Unit?): Bitmap? {
  11.         Log.d(TAG, "loadPhoto: Started")
  12.         Log.d(TAG, src)
  13.         val url: URL = URL(src)
  14.         //val url: URL = URL("https://cdn.wallpapersafari.com/65/31/aAzTvc.jpg")
  15.         var connection: HttpURLConnection? = null
  16.         var inputStream: InputStream? = null
  17.         var bitmap: Bitmap? = null
  18.  
  19.         try {
  20.             connection = url.openConnection() as HttpURLConnection
  21.             connection.doInput
  22.             connection.connect()
  23.             inputStream = connection.inputStream
  24.             val options = BitmapFactory.Options()
  25.             options.inPreferredConfig = Bitmap.Config.RGB_565
  26.             bitmap = BitmapFactory.decodeStream(inputStream, null, options)
  27.         } catch (e: Exception) {
  28.             Log.d(TAG, "${e.cause} ${e.message}")
  29.         } finally {
  30.             connection?.disconnect()
  31.             inputStream?.close()
  32.         }
  33.         return bitmap
  34.     }
  35.  
  36.     override fun onPostExecute(bitmap: Bitmap?) {
  37.         if (bitmap != null) {
  38.             val imageView: ImageView? = imageViewReference.get();
  39.             imageView?.setImageBitmap(bitmap)
  40.         } else {
  41.             Log.d(TAG, "I hate all this shit")
  42.         }
  43.     }
  44. }
  45.  
  46.  
Advertisement
Add Comment
Please, Sign In to add comment