I want to get an image as bytes array from an external URL in Scala. What's the way to do the same?
val url = "https://lh3.googleusercontent.com/jUoaTIlBn5ibfQcND2n5OMD6Z7xoqNj-ShHlFR6QuLffLXD5pS8V2eNg1rGlrsRrnDkoQ28O8UHzqzBQKAGY4l1CS2NQSq2SkRScK6FOjl82jppyohK-"
def getImageFromUrlInBytes = (url:String):Array[Bytes] = ???
CodePudding user response:
Using Scala 2.13 and Java 9 :
def getImageFromUrlInBytes(urlString: String): Array[Byte] = {
Using.resource(new URL(urlString).openStream()) { in =>
in.readAllBytes()
}
}
InputStream.readAllBytes
is a useful method of Java 9 .