how to stop loading images

19 views
Skip to first unread message

wael ali

unread,
Dec 30, 2017, 7:14:25 AM12/30/17
to CodenameOne Discussions
Hello,
i am working on an application where it loads images from website (representing products)
when click on an image it should go to a new function which load details of product and allow to buy
my problem is the first page will not handle any clicks until all images are loaded

i am using the below code to load the images from the website 

    EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(300, 400, 0xffff0000), true);     
    URLImage background = URLImage.createToStorage(placeholder, name1+"-300_400.jpg",URL+"/images/"+name1); 
    background.fetch();
    Label imagelabel=new Label();
    imagelabel.setIcon(background);

how i can stop the background fetch when click

thank you

wael ali

unread,
Dec 30, 2017, 7:16:35 AM12/30/17
to CodenameOne Discussions
how i can stop the fetch when click

thank you

Shai Almog

unread,
Dec 30, 2017, 11:53:22 PM12/30/17
to CodenameOne Discussions
Hi,
You can use the connection request directly and use the kill() method on that to stop the download. URLImage hides implementation details like this and doesn't currently provide a way to cancel a download.

wael ali

unread,
Dec 31, 2017, 6:27:10 AM12/31/17
to CodenameOne Discussions
Hello Shai,
thank you for the reply
could you provide me an example also how i know which one is finished or which is not
or should i kill all
or is there a better way to show image from internet?
thank you

Shai Almog

unread,
Jan 1, 2018, 12:00:21 AM1/1/18
to codenameone...@googlegroups.com
Hi,
something like:

ConnectionRequest q = new ConnectionRequest(imageUrl, false);
q
.downloadImageToStorage(imageFile, img -> label.setIcon(img));
addToQueue
(q);

// then when you want to kill
q
.kill();


wael ali

unread,
Jan 1, 2018, 3:34:54 AM1/1/18
to CodenameOne Discussions
Hello Shai,

thank you again for the help
the problem i have a for loop for a json returned and from it i do the urlimage lead
in this case i need to keep track of all connections created to be able to kill them all
or is there a way to kill all

thank you

Shai Almog

unread,
Jan 2, 2018, 12:18:58 AM1/2/18
to CodenameOne Discussions
Hi,
as I mentioned before URLImage is problematic for this. It seamlessly downloads an image and part of that seamlessness means you don't have a reference to the connection request used.
So you don't get access to the kill method. You could kill all the connections there are (including unrelated connections you might have) in the queue through the network manager but that's probably not a good idea.

The method I highlighted provides you full control, you can just keep a reference to the pending connections and remove them once they complete.
Reply all
Reply to author
Forward
0 new messages