What would be nice if there's a way to simply get ...
# android
a
What would be nice if there's a way to simply get all images in a file or all available resource ids for resource. I know Kotlin has a "walking" function to grab names of files
g
get all images in a file
What do you mean?
a
get all images from a specific file in resource like drawable
g
Do you mean load all the images from directory or tree of directories?
a
it's quite possible that I just wasn't successful trying to use this for the drawable folder.
g
It doesn’t make sense for resources, because all drawable resources always flat, there is not tree or even directory
a
@gildor I would ideally like to walk through one direction at the first level to grab all images
g
It’s impossible for resources, you can group them by id (for example using int array resource)
a
I had a feeling it was due to restribtions
g
Of course, you can also ship your images as raw file in
raw
resource dir
but I just curious about your case
a
btw, you can walk through files in a directory without recursion
g
of course. File(“someDir”).listFiles()
a
oh yeah sure - just wanted to create a quick simple android with Kotlin. It's cards of images sitting in a grid to display an art portfolio
@gildor haha but just to be clear, a file walk definitely cannot be done for a resource file? I'm best off using a raw file or just setting the id's in an array?
I guess I could do that. I just always feel weird having to hardcode resources with the array approach
g
Just put all your card images to resources then use array to group them:
Copy code
<array name="cards">    <item>R.drawable.clubs</item>
   <item>R.drawable.hearts</item>
</array>
It’s just much easier to work with resources than with raw files and pass cached bitmaps instead of resource id.
a
I have them all sitting in /res/drawable at the moment. I didn't realize an array could occur in xml
I was thinking about how annoying it is, but that's probably why it's better to pull images from an API in the first place. I'm thinking that's going to be the next phase, I could maybe pull it off my dropbox
@gildor atm, I have one image sitting in an appwidget-provider. I could put those into an array.
g
Then you can easily load all the drawables:
Copy code
fun loadDrawables(arrayId: Int, res: Resources): List<Drawable> {
    val icons = res.obtainTypedArray(arrayId)
    val list = (0 until icons.indexCount).map {
        icons.getDrawable(it)
    }
    icons.recycle() // Must recycle array after
    return list
}
👍 1
arrow 1
walk definitely cannot be done for a resource file
But resources are flat, there is no directories, you probably can iterate through all the app resources, but it’s probably not the thing that you want to do, you want to iterate through specific group of resources
There is no standard Android way to group resources using directories, even if you have a few resources dirs (you can do that using gradle) on compile time all those res directories values will be merged and resources will be flatten
👍 1
probably why it’s better to pull images from an API in the first place
This is good question. Usually if images are big you load them from API, but for some types of apps it doesn’t make any sense, if those resources are static and required to run you app. Sometimes those images just dynamic (so you want to change them without app update) in this case load from network of course better solution. It’s always trade off, but I don’t think that grouping of images is somehow good argument to chose one of possible options
👍 1
And if you load images from network one of those image downloading libraries would be very helpful, it’s really not so easy to write your own, just many pitfalls and features to implement
a
agreed! that solution you wrote is an acceptable one for the circumstances, even taking care of the memory leak at the end. I appreciate that
g
There are some use cases to load local resources using libraries like Picasso, because you can use image processors api or load resources with required resolution. But if it’s not your case, just use local resources directly, it’s easier and more efficient
Also add
@ArrayRes
annotation to arrayId argument, it will decrease change of passing of different type resource id and function signature will be easier for understanding
👍 1
a
Ah - now that makes a lot more sense for such a use case. Nah, mine is just a quick and dirty approach to try out Kotlin with Android. I'm a little rusty, and while I REALLY love Kotlin, I ended up spending time stumbling over the XCode-like features Android Studio has 😆
I really appreciate the direction with it though - I've always seemed to struggle a little with resources but that explanation really cleared it up for me. I'll get back to you tomorrow on it (it's 1:30AM over here) and I definitely will make use of the annotations!
g
Sure 👍
290 Views