Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use with JsonReader? #1270

Closed
AndroidDeveloperLB opened this issue Jun 16, 2019 · 22 comments · Fixed by #1279
Closed

How to use with JsonReader? #1270

AndroidDeveloperLB opened this issue Jun 16, 2019 · 22 comments · Fixed by #1279

Comments

@AndroidDeveloperLB
Copy link

On v3.0.3 , I could use this:

        val reader = android.util.JsonReader(BufferedReader(InputStreamReader(FileInputStream(File(filePath)))))
        animationView.setAnimation(reader, null)

But when I've recently updated to 3.0.6 , it says I need to use com.airbnb.lottie.parser.moshi.JsonReader instead.

It doesn't seem like the normal JsonReader , and I can't pass it what I've passed to it.

How can I use the new JsonReader of Lottie? What should I do?

@gpeal
Copy link
Collaborator

gpeal commented Jun 17, 2019

@AndroidDeveloperLB Hmm, this was an unintended consequence of us switching out the underlying JsonReader for performance and to fix some OEM specific crashes. I'll expose the InputStream version then you can do setAnimation(FileInputStream(...))

@AndroidDeveloperLB
Copy link
Author

What is the best way to handle the InputStream ? Is this currently possible ?
Does it close it automatically, or do I have to close it myself?

@gpeal
Copy link
Collaborator

gpeal commented Jun 17, 2019

You can use LottieCompisitionFactory.fromInputStream(InputStream, cacheKey) and yes, it will close it.

@AndroidDeveloperLB
Copy link
Author

AndroidDeveloperLB commented Jun 18, 2019

You probably mean LottieCompositionFactory.fromJsonInputStream(FileInputStream(File(filePath)), null)

But what do I do with it exactly? It looks like a background task.

I want to set the animation of the LottieAnimationView to use it.
There is no animationView.setAnimation(...) to use what's coming from this function...

image

All I want is that it will load and show the animation from the inputStream. Let it load later however it needs in the background.

@gpeal
Copy link
Collaborator

gpeal commented Jun 21, 2019

@AndroidDeveloperLB You can add a listener on the task then call animationView.setComposition I'll expose better APIs through LottieAnimationView in a future version to simplify/wrap this but for now, that will work just fine.

@AndroidDeveloperLB
Copy link
Author

I want to set the animation and let the View handle it, just like any other source of animation.
If there is a new animation to set for it, the previous one will be cancelled.
At least, that's how it worked so far, no?
Meaning that if I had called animationView.setAnimation(whatever...) and soon after I called animationView.setAnimation(whatever2...) , it will ditch what it had tried to load before.

If I use a listener, I will need to somehow manage this situation, because I can't just tell it to set the animation when it's done, as there might be a different animation being set between the time I started to the time I got a callback from the listener.
This requires extending the animation view of Lottie.

Can you please show how to use what you wrote?

@gpeal
Copy link
Collaborator

gpeal commented Jun 22, 2019

@AndroidDeveloperLB As I mentioned, I am going to add an InputStream version directly on LottieAnimationView

@gpeal
Copy link
Collaborator

gpeal commented Jun 22, 2019

In the mean time, you could also convert it to a String and pass the json string in. Not ideal but I'll fix this soon.

@AndroidDeveloperLB
Copy link
Author

Can you please let me know after it gets fixed?

gpeal added a commit that referenced this issue Jun 23, 2019
… hide visibility of the Lottie Moshi JSON parsing classes. (#1279)

This PR also hides the visibility of the Lottie Moshi JSON parsing classes.
Fixes #1270
@AndroidDeveloperLB
Copy link
Author

AndroidDeveloperLB commented Jun 23, 2019

So which version to use, that allows to use InputStream?
The release page hasn't updated since 3.0.7 :
https://github.com/airbnb/lottie-android/releases

@gpeal
Copy link
Collaborator

gpeal commented Jun 23, 2019

The next release, it'll likely by 3.1.0. I hope to release it this week.

@AndroidDeveloperLB
Copy link
Author

AndroidDeveloperLB commented Jun 23, 2019

What's the best way for now, then, to set animation from File (or file path) ?

@AndroidDeveloperLB
Copy link
Author

Is it this:

animationView.setAnimationFromUrl(filePath)

?

@gpeal
Copy link
Collaborator

gpeal commented Jun 23, 2019

Right now, read the file as a string then pass it to setAnimationFromJson. I'll make a new release this week though.

@AndroidDeveloperLB
Copy link
Author

Does setAnimationFromUrl work fine with filePath as I wrote?

@gpeal
Copy link
Collaborator

gpeal commented Jun 23, 2019

@AndroidDeveloperLB No because a file is not a network url.

@AndroidDeveloperLB
Copy link
Author

@gpeal I see. In the docs they say as if it's a normal file, so I thought it can be used too.

@AndroidDeveloperLB
Copy link
Author

@gpeal So for now it's possible to do as such:

            val jsonStr = IOUtils.toString(BufferedReader(InputStreamReader(FileInputStream(File(filePath)))))
            animationView.setAnimationFromJson(jsonStr, null)

?
Sadly this is on the UI thread, but should work fine for now on tiny files...

@gpeal
Copy link
Collaborator

gpeal commented Jul 1, 2019

@AndroidDeveloperLB or LottieCompositionFactory.fromInputStream(...)

@AndroidDeveloperLB
Copy link
Author

@gpeal There is no such thing:

image

And if you mean fromJsonInputStream instead, I don't see how to set it to the animationView :

image

@gpeal
Copy link
Collaborator

gpeal commented Jul 6, 2019

@AndroidDeveloperLB Yes, fromJsonInputStream. Add a success listener to that and call setComposition on your LottieAnimationView with the result.

@AndroidDeveloperLB
Copy link
Author

AndroidDeveloperLB commented Jul 7, 2019

I see. Sadly due to this issue:
#1298
I went back to v3.0.3 .
v3.0.7 caused a lot of issues for users due to this crash (or at least so it seems for now).

I hope next version will fix this, and also provide a nicer way to handle what I wrote here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants