1

I have the following code -

 verifyOtpViewModel.userData.observe(this, Observer {
            if (it == null) return@Observer
            if (it.profileImage != null) {
                ...
            }
}

profileImage is my image URL.

I need an updated way, by 2020 standard to get the image bitmap from the URL and then get the URI from that bitmap.

All answers on this subject use the soon-to-be deprecated AsyncTask and I was hoping for a better solution, maybe a 3rd party library even.

Thanks!

Alon Shlider
  • 1,187
  • 1
  • 16
  • 46
  • Since a URL is a URI... you don't need to get the image bitmap from the URL. Just use the URL. Otherwise, please explain in detail what is special about the URI and why it needs to be different than the URL. – CommonsWare Apr 26 '20 at 10:51
  • @CommonsWare The URL represents an image in which I need to show to the UI each time the app comes up live. Since I want to do less network calls and make the image load faster I want to save the image in my Room DB and load it much faster. – Alon Shlider Apr 26 '20 at 10:54
  • @CommonsWare What I ment was a web URL vs an interal file URI – Alon Shlider Apr 26 '20 at 11:03
  • 1
    Why not just [use Glide with a disk cache](https://bumptech.github.io/glide/doc/configuration.html#disk-cache) and save the URL itself? That way, if your cache gets cleared, you can fetch the image from the network again. If you are sure that you want to save it durably, just download the content to a file [using OkHttp](https://stackoverflow.com/a/29012988/115145) to a file in `getFilesDir()`, then save the path to the file in your database. – CommonsWare Apr 26 '20 at 11:07

1 Answers1

0

First download the image if it's not already downloaded

GlideApp is the generated API:

Add a class with exact same name. Be sure to Rebuild after adding this code snippet:

you need to use kapt instead of annotactionProccesser on Kotlin.

@GlideModule
class MyAppGlideModule : AppGlideModule()

diskCacheStrategy(DiskCacheStrategy.NONE) don't let Glide cache this image. Target.Size_Original keep the original size when you download the image from url.

GlideApp
            .with(context)
            .asBitmap()
            .load(uri)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .into(object : CustomTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                override fun onLoadCleared(placeholder: Drawable?) {}
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                       //Save image file
                       saveFile(resource)
                       //Load into imageview if needed
                    }
                }
            })

Save the image file. It's recommended to use background threads for this.

val randomFilename = //generate a name based on your preferences
val file = File(saveDirectory, randomFilename)
val fos = FileOutputStream(file)
val bos = BufferedOutputStream(fos)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos)
bos.close()

Update your room database with the name of the file to retrieve that later.

@Query("UPDATE table_name SET image_filename = :fileName WHERE id = :id ")
fun updateImageFilename(id: Int, filename: String)
Arrowsome
  • 2,649
  • 3
  • 10
  • 35