0

I'm trying to write a function in Kotlin that needs to return a Bitmap after getting a stream from a StorageReference object. Since I can't use a solution that implements listeners I have to define a Continuation and get the result when the StreamDownloadTask is completed.

The following is my not working attempt

fun getAdImg(imgId: String): Bitmap? {
        return storage.child(FOLDER).child(imgId+EXTENSION).stream.continueWith(Continuation<StreamDownloadTask.TaskSnapshot, Bitmap>() {
            return BitmapFactory.decodeStream(it.result)
        })

    }

I cannot understand how to define the Continuation object as there are no examples anywhere using basic types. I guess I have to override the then function and check if the Task has been completed in order to fetch the result.

EDIT1: The error says:

Type mismatch. Required: Bitmap? Found: Task<Bitmap!>

Antonio Santoro
  • 827
  • 1
  • 11
  • 29
  • Are you saying that what you have written now doesn't work? Is there an error message? Please edit the question to be specific. If you're trying to make the download synchronous and return the converted bitmap value from getAdImg, that's not going to work at all. You will have to use a callback or coroutine. – Doug Stevenson May 30 '20 at 16:37
  • Type mismatch Required: Bitmap? Found: Task – Antonio Santoro May 30 '20 at 16:38
  • You will need to use that Task asynchronously. There is no avoiding that. You will not be able to make the function return a Bitmap unless you make the method block, which is probably a bad idea. This is what callbacks and coroutines are for. – Doug Stevenson May 30 '20 at 16:51
  • My idea was to make this function as a `suspend function` since it will be launched from a view model scope but I need it to return the result I need but I'm not able to – Antonio Santoro May 30 '20 at 16:55
  • OK, if you want to make this a suspend fun, then go ahead and do that, then update the question with what you've tried that doesn't work, along with an explanation. – Doug Stevenson May 30 '20 at 16:57
  • As I said I'm not able to return a `Bitmap` since I have no clue on how to create the `Continuation` object inside the `continueWith` function – Antonio Santoro May 30 '20 at 16:59
  • If your goal is to create an asynchronous suspend fun, then your question should be updated to state that goal. I've already said that it's a bad idea to try to return the Bitmap synchronously, given that the Firebase APIs are all asynchronous. If you **do** want to create a suspend fun, then a continuation isn't necessary here at all. You can use the play services task adapter. https://github.com/Kotlin/kotlinx.coroutines/tree/master/integration/kotlinx-coroutines-play-services – Doug Stevenson May 30 '20 at 17:02
  • I'm sure I have setup everything correctly but there is no `await()` method on `Task` like in the example of that link – Antonio Santoro May 31 '20 at 11:41
  • You have to import the extension function from the library. – Doug Stevenson Jun 02 '20 at 17:02

0 Answers0