How can you get an Image object from a URL?

105 views
Skip to first unread message

bjy...@gmail.com

unread,
Jul 21, 2013, 11:30:21 AM7/21/13
to codenameone...@googlegroups.com
I am trying to get a Codename One Image object from a URL.

Image i = EncodedImage.create(new URL(imageurl).openStream());

throws a "Error: cannot find symbol: class URL" exception. any ideas?

Shai Almog

unread,
Jul 21, 2013, 2:00:33 PM7/21/13
to codenameone...@googlegroups.com, bjy...@gmail.com
URL doesn't exist in Codename One since networking is too different on other platforms (Windows Phone is really extremely different but iOS is also pretty hairy).

You can use something like:
ConnectionRequest r = new ConnectionRequest();
r
.setUrl(imageURL);
r
.setPost(false);
NetworkManager.getInstance().addToQueueAndWait(r);
EncodedImage.create(r.getResultData());

But you would be better off using ImageDownloadService which will do far more including binding to the UI, caching, resizing etc...

Unlike in the code you typed this will do everything in a background thread, handle all exceptions and properly cleanup all streams.
Reply all
Reply to author
Forward
0 new messages