how would I go about loading an image from a test ...
# getting-started
c
how would I go about loading an image from a test directory's resources in kotlin? Tried it using the class loader but ran into a NPE:
Copy code
var byteArray = this::class.java.classLoader.getResource("abc.jpg").readBytes()
j
Not sure if the folder should be named
resources
, not
res
?
c
not sure it matters, it's marked as resource directory and the one I use for the src dir has the shortened name too
t
What build system are you using? Anything like Gradle? Generally speaking you can use:
Copy code
var byteArray = this::class.java.classLoader.getResource("/abc.jpg").readBytes()
(Note the absolute path of the resource)
Generally speaking the
res
directory should be named
resources
, which is the convention for such directories. If you are using gradle, this is the one being picked up by gradle while building and running your code.
c
Yea, Gradle w the Kotlin DSL. I'll try that again but I think I experimented with both to no avail. It's a KMM project if that makes a difference, currently only working on the androidApp module though
t
So test 1. Having a absolute path (
/abc.jpg
) 2. Rename
res
to
resources
If its still not working, just ping me and we ll see if I can help you somehow.
c
Oh actually changing the name from res seems to have fixed it, without absolute path. Thanks!
👍 2
n
Without the starting slash in the path it will be relative to the class doing the lookup, so e.g. if you are loading from com.example.app.MyClass then the file would need to be at
resources/com/example/app/abc.jpg
☝️ 1