Aasiya3 / ElephantBook_FieldGuide

Apache License 2.0
1 stars 1 forks source link

Add image download functionality to the ApiGetter class #27

Closed bsakai2000 closed 2 years ago

bsakai2000 commented 3 years ago

Probably using Volley ImageLoader/ImageListener (https://afzaln.com/volley/com/android/volley/toolbox/ImageLoader.html) to download bitmaps, then using local storage (#5) to store them in the filesystem

bsakai2000 commented 3 years ago

Testing code downloads and displays Google logo, presumably the download portion of this will work for us once I get it into an ApiGetter method

val queue = Volley.newRequestQueue(this)
        val imgLdr = ImageLoader(queue, object : ImageLoader.ImageCache {
            val cache = LruCache<String, Bitmap>(50)
            override fun getBitmap(url: String?): Bitmap? {
                return cache.get(url)
            }
            override fun putBitmap(url: String?, bitmap: Bitmap?) {
                cache.put(url, bitmap)
            }
        })
        val elephantArrReq = imgLdr.get(
            "https://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png",
            object : ImageLoader.ImageListener {
                override fun onResponse(
                    response: ImageLoader.ImageContainer,
                    isImmediate: Boolean
                ) {
                    if (isImmediate && (response?.bitmap == null)) return
                    applicationContext.openFileOutput("path.png", Context.MODE_PRIVATE).use {
                        val stream = ByteArrayOutputStream()
                        response.bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
                        it.write(stream.toByteArray())
                    }
                    val myDraw = Drawable.createFromStream(applicationContext.openFileInput("path.png"), "path.png")
                    println(myDraw)
                    simpleImageView.setImageDrawable(myDraw)
                }

                override fun onErrorResponse(err: VolleyError) {
                    println(err)
                }
            }
        )
bsakai2000 commented 3 years ago

General flow should be a call from the UI to DatabaseWrapper.updateDatabase(), which should call some helper function like private fun DatabaseWrapper.updateProfilePictures(), which can iterate through the list of Elephant pfps and use something like ApiGetter.downloadImage(url: String, path: String) for each

Need to check which images are present in filesystem already (#19), could be in downloadImage but probably better to handle it in updateProfilePictures, so it won't be covered by this issue. Probably using some combination of context.fileList() and context.getDir() but not sure how we access those.

bsakai2000 commented 3 years ago

Also should probably refactor the VolleyQueue into a constructor-time class attribute rather than re-generating the queue on every request, especially now that we're using it from multiple places in ApiGetter

bsakai2000 commented 2 years ago

Probably using some combination of context.fileList() and context.getDir() but not sure how we access those.

Side-stepped the issue by flattening our filesystem. Replace path separator / with underscore _ when storing in the filesystem. This means everything is in the top-level directory and we don't have to worry about getDir()