Skip to content

Instantly share code, notes, and snippets.

@alz-ahm
Created March 15, 2019 12:46
Show Gist options
  • Save alz-ahm/12d29f8fdddefd9a331fb0e5091301e2 to your computer and use it in GitHub Desktop.
Save alz-ahm/12d29f8fdddefd9a331fb0e5091301e2 to your computer and use it in GitHub Desktop.
//Withoug extension
Picasso.get().load(/* image url */).placeholder(/* placeholder */).into(myImageView)
//With extension
myImageView.load(/* image url */)
//Extension definition
fun ImageView.load(path: String) {
val placeholder = R.id.whatEver
var fullPath = path
if(path.isBlank()){
setImageResource(placeholder)
return
}
//if its not from the server, it a file from file system and we have to append file prefix otherwise
//picasso won't be able to load the image.
if(!path.startsWith("http", true))
fullPath = "file://$path"
Picasso.get().load(fullPath).placeholder(placeholder).into(this)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment