question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature request] initialize a LottieAnimationView with a remote url directly

See original GitHub issue

Would like to have a way to initialize a LottieAnimationView with a remote url directly, which is available in lottie-ios as initWithContentsOfURL.

In the sample app, private void loadUrl(String url) { ... } loads the json file and passes the parsed json string to the private void loadJsonString(String jsonString) { ... } method. Why not put that logic inside lottie?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13

github_iconTop GitHub Comments

3reactions
AndroidDeveloperLBcommented, Jul 31, 2018

OK never mind. Basic usage can be as such:

@SuppressLint("ObsoleteSdkInt")
class LottieAnimationViewEx : LottieAnimationView {
    var loadingThread: Thread? = null

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    init {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            useHardwareAcceleration(true)
    }

    interface OnAnimationReadyListener {
        /**called when the animation is ready, or can't be accessed due to some error (null in this case)*/
        fun onAnimationReady(inputStream: InputStream?)
    }

    @UiThread
    fun loadAnimationFromUrl(url: String, onAnimationReadyListener: OnAnimationReadyListener? = null) {
        val handler = Handler()
        loadingThread = object : Thread() {
            override fun run() {
                super.run()
                val streamForUrl = StreamUtil.getInputStreamFromUrl(url)
                handler.post {
                    if (onAnimationReadyListener == null) {
                        if (streamForUrl == null)
                            return@post
                        val reader = JsonReader(BufferedReader(InputStreamReader(streamForUrl)))
                        setAnimation(reader)
                    } else onAnimationReadyListener.onAnimationReady(streamForUrl)
                }
            }
        }
        loadingThread!!.start()
    }
}



public static InputStream getInputStreamFromUrl(final String urlStr) throws IOException {
    if (TextUtils.isEmpty(urlStr))
        return null;
    InputStream input = null;
    HttpURLConnection connection = null;
    final URL url = new URL(urlStr);
    connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(CONNECTION_TIMEOUT_IN_MILLIS);
    connection.setReadTimeout(CONNECTION_TIMEOUT_IN_MILLIS);
    // See the http://stackoverflow.com/questions/15411213/android-httpsurlconnection-eofexception for
    // explanations
    connection.setRequestProperty("Connection", "close");
    // this is an attempt to overcome an error of "FileNotFoundException"
    connection.setDoOutput(false);
    connection.connect();
    // expect HTTP 200 OK, so we don't mistakenly save error report
    // instead of the file
    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
        return null;
    // download the file
    input = connection.getInputStream();
    return input;
}

Could be nice to know how to do it more nicely though.

1reaction
rajivrnaircommented, Jun 5, 2020

Yep. Been using it for a couple of days and no issues so far.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lottie Docs
Lottie is a library for Android, iOS, Web, and Windows that parses Adobe After Effects animations exported as json with Bodymovin and renders...
Read more >
How to use lottie animation in flutter app? - Stack Overflow
It is a direct port of Lottie-Android and support the same set of features. ... Load a Lottie file from a remote url...
Read more >
Using Lottie Android · Lottie Documentation - salihabdul-karim1
The sample app includes some built in animations but also allows you to load an animation from internal storage or from a url....
Read more >
How to Implement Dynamic Loader with Lottie and Firebase
We will first create AnimationView instance and load it with default fail safe loader. We will first download the Lottie URL from Remote...
Read more >
androidtutorial – Be Thinker, Be Coder
Hello Friends, Here I Make Imageview for use in Sticker functionality. It's Apply For only Imageview,. How to Use this : In XML...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found