Search code examples
androidlithofacebook-litho

Litho image from remote URL


I am trying to load an image from remote URL into Litho Image widget but Litho widget has "drawable" as the only prop to set image. Have any one tried to set image from remote URL inside Litho Image widget?


Solution

  • If you really want to use Litho, you can download the image, and convert it to a Drawable object.

    public static Drawable drawableFromUrl(String url) throws IOException {
        Bitmap b = null;
    
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();
    
        b = BitmapFactory.decodeStream(input);
    
        return new BitmapDrawable(b);
    }
    

    Note that you need to call this method in a seperate Thread or AsyncTask since this is a network operation.