[Help] How to load an image from the web and put it into a Image object.

81 views
Skip to first unread message

Roman

unread,
May 31, 2015, 10:26:40 PM5/31/15
to codenameone...@googlegroups.com
Hi! i write you to ask how i can load a remote image and put it into a Image variable.

i tried to do it, but it never loads the image

my code is:

[code]

                EncodedImage EI = EncodedImage.createFromImage(EncodedImage.createImage(48, 48), true);
                   Image tmp =fetchResourceFile().getImage("circViajesBuscar.png");
                   EncodedImage placeholder = EncodedImage.createFromImage(tmp, false);
                   URLImage imageurl =  URLImage.createToStorage(EI, "sj4p2.png", "http://i.stack.imgur.com/sj4p2.png", URLImage.RESIZE_SCALE);
      
             imageurl.fetch();

[code]

Shai Almog

unread,
Jun 1, 2015, 1:26:58 AM6/1/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com, rocotoc...@gmail.com
Hi,
don't invoke fetch.
Did you add the image to a label "as is" or did you do something creative like placing it in a renderer?
You can look at the simulators network monitor to see if the request went out and if it completed successfully.

rocotoc...@gmail.com

unread,
Jun 1, 2015, 7:40:28 PM6/1/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com
Hi! thanks

No, my idea is to load into a variable to reference it in a multibutton.

may be, you can sugest me another way. The efect that im looking for is  the same that you use in the application of the properties.

thanks in advance!

Steve Hannah

unread,
Jun 1, 2015, 7:56:09 PM6/1/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com
I just fixed a bug with EncodedImage today that may be affecting your use-case here.  It hasn't hit the build server yet, but should be up soon (my guess in in a few hours -- or whenever Shai wakes up ).

Steve

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/6f8480c7-7edb-4faf-b99e-6244b0d2fffa%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Software Developer
Codename One

rocotoc...@gmail.com

unread,
Jun 1, 2015, 8:09:18 PM6/1/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com
ok, so my code is right??

rocotoc...@gmail.com

unread,
Jun 1, 2015, 8:25:38 PM6/1/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com
Shai:
   i check in to the simulators network monitor and i cant see the request :(



On Monday, June 1, 2015 at 2:26:58 AM UTC-3, Shai Almog wrote:

Steve Hannah

unread,
Jun 1, 2015, 8:46:40 PM6/1/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com
You haven't posted enough code for me to know whether your code is right.   The portion that you posted should be fine - but it depends, then, on what you are doing with the image.

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.

For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Web Lite Solutions Corp.

rocotoc...@gmail.com

unread,
Jun 1, 2015, 10:26:55 PM6/1/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com

here is the whole procedure. please, help to retrieve the image from a url and to put it into an image variable


   protected void cargarItemsNivel(int nivel)
    {
       
        System.out.println("cargarItemsNivel()");
   
              ConnectionRequest r;
        r = new ConnectionRequest()
        {
           

            @Override
            protected void readResponse(InputStream input) throws IOException
            {
//        InfiniteProgress prog = new InfiniteProgress();
 //       Dialog dlg = prog.showInifiniteBlocking();


     final String url = "http://www.personallinks.com.ar/imagenes/slider/villaVentania.jpg";
             
     

               
               
    //    Display.getInstance().getCurrent().getContentPane().removeAll();
                InputStreamReader reader = new InputStreamReader(input);
                JSONParser parser = new JSONParser();
                Hashtable response = parser.parse(reader);
                Vector  registros = (Vector) response.get("root");
               
                Component c =  Display.getInstance().getCurrent();

                /* ---- i tried to load the image into the label , but it didnt work .. why? */   
                Label myLabel = (Label) findByName("Label",c);
                ImageDownloadService.createImageToStorage(url, myLabel,"222222", new Dimension(20,20));
                /*---------------------------------------------------------------------------*/
               
                List objLista = (List) findByName("ListItems",c);

                Vector vec = new Vector();
                
                for (int i=0; i<registros.size();i++)
                {
                    Hashtable  registro = (Hashtable)registros.elementAt(i);
                    String descripcion = (String)registro.get("$descripcion");
                    Hashtable h = new Hashtable();
                    h.put("LblCaption",descripcion);

                   
                   /* In the next code i try to load the image , but never loads and shows the image from de resource file. */

                   
                   Image tmp =fetchResourceFile().getImage("circViajesBuscar.png");
                    EncodedImage placeholder = EncodedImage.createFromImage(tmp, false);
                   Image tmp2 =   URLImage.createToStorage(placeholder, "myimagen.jpg", "http://www.personallinks.com.ar/imagenes/slider/villaVentania.jpg", URLImage.RESIZE_SCALE_TO_FILL);

                   /** the next way also doesnt work***************/

                   EncodedImage EI = EncodedImage.createFromImage(EncodedImage.createImage(48, 48), true);
                   Image imageurl =  URLImage.createToStorage(EI, "sj4p2.png", "http://i.stack.imgur.com/sj4p2.png", URLImage.RESIZE_SCALE);
                    /*******************/                           

                   MultiButton mb = new MultiButton();
                   mb.setTextLine1(descripcion);
                   mb.setIcon(tmp);
                    Display.getInstance().getCurrent().getContentPane().addComponent(mb);
                }
       Display.getInstance().getCurrent().getContentPane().repaint();
      myLabel.repaint();

            }
        };

        r.setUrl("http://www.personallinks.com.ar/basedoble/wyn.php");
        r.setPost(false);
        r.addArgument("task", "getNivel");
        r.addArgument("uid", "0");
        InfiniteProgress prog = new InfiniteProgress();
        Dialog dlg = prog.showInifiniteBlocking();
        r.setDisposeOnCompletion(dlg);
        NetworkManager.getInstance().addToQueue(r);

 
  
    }
   

On Sunday, May 31, 2015 at 11:26:40 PM UTC-3, Roman wrote:

rocotoc...@gmail.com

unread,
Jun 1, 2015, 11:26:36 PM6/1/15
to codenameone...@googlegroups.com
                   Image tmp =fetchResourceFile().getImage("circViajesBuscar.png");
                    EncodedImage placeholder = EncodedImage.createFromImage(tmp, false);
                   Image tmp2 =   URLImage.createToStorage(placeholder, "myimagen.jpg", "http://www.personallinks.com.ar/imagenes/slider/villaVentania.jpg", URLImage.RESIZE_SCALE_TO_FILL);

                   /** the next way also doesnt work***************/
                   EncodedImage EI = EncodedImage.createFromImage(EncodedImage.createImage(48, 48), true);
                   Image imageurl =  URLImage.createToStorage(EI, "sj4p2.png", "http://i.stack.imgur.com/sj4p2.png", URLImage.RESIZE_SCALE);
                    /*******************/                           

                   MultiButton mb = new MultiButton();
                   mb.setTextLine1(descripcion);
                   mb.setIcon(tmp);
                    Display.getInstance().getCurrent().getContentPane().addComponent(mb);
                }
       Display.getInstance().getCurrent().getContentPane().repaint();
      myLabel.repaint();

            }
        };

        r.setUrl("http://www.personallinks.com.ar/basedoble/wyn.php");
        r.setPost(false);
        r.addArgument("task", "getNivel");
        r.addArgument("uid", "0");
        InfiniteProgress prog = new InfiniteProgress();
        Dialog dlg = prog.showInifiniteBlocking();
        r.setDisposeOnCompletion(dlg);
        NetworkManager.getInstance().addToQueue(r);
 
  
    }
   






On Monday, June 1, 2015 at 9:46:40 PM UTC-3, shannah wrote:
You haven't posted enough code for me to know whether your code is right.   The portion that you posted should be fine - but it depends, then, on what you are doing with the image.
On Mon, Jun 1, 2015 at 5:25 PM, <rocotoc...@gmail.com> wrote:
Shai:
   i check in to the simulators network monitor and i cant see the request :(


On Monday, June 1, 2015 at 2:26:58 AM UTC-3, Shai Almog wrote:
Hi,
don't invoke fetch.
Did you add the image to a label "as is" or did you do something creative like placing it in a renderer?
You can look at the simulators network monitor to see if the request went out and if it completed successfully.

--
--

Steve Hannah

unread,
Jun 1, 2015, 11:29:47 PM6/1/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com
You shouldn't be using the UI from the readResponse() method (as it runs on the network thread - not the EDT).  Use postResponse() for interacting with the UI. 

Other than that, I think the code looks ok.

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.

For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah

rocotoc...@gmail.com

unread,
Jun 2, 2015, 8:53:56 PM6/2/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com
Hi! again! i modify my code moving the UI to postResponse but still not working.

Plese , can you explaind me how to do it works?

thanks!

my new code is:

[CODE]

    protected void cargarItemsNivel(int nivel)
    {
        System.out.println("cargarItemsNivel()");
                final Component c =  Display.getInstance().getCurrent();

   
              ConnectionRequest r;
        r = new ConnectionRequest()
        {
            @Override
            protected void postResponse()
            {
                Display.getInstance().getCurrent().getContentPane().removeAll();   
               
                /* ---- The next code doesnt work. Doesnt show the image beside the label. Why? */   
                Label myLabel = new Label();
                myLabel.setText("Hola!!!");

                ImageDownloadService.createImageToStorage(url, myLabel,"222222", new Dimension(20,20));
                Display.getInstance().getCurrent().getContentPane().addComponent(myLabel);
                /*---------------------------------------------------------------------------*/
  
               
                   /* The next code works, but because im loading an image from the resources */

                   
                   Image tmp =fetchResourceFile().getImage("circViajesBuscar.png");
                   MultiButton mb = new MultiButton();
                   mb.setTextLine1("1");
                   mb.setIcon(tmp);
                   Display.getInstance().getCurrent().getContentPane().addComponent(mb);

                   /* The next code, shows the image from the resources, but doesnt show the image*/
                   /* How is the right code?*/

                  
                   EncodedImage placeholder = EncodedImage.createFromImage(tmp, false);
                   Image tmp2 =   URLImage.createToStorage(placeholder, "myimagen.jpg", "http://www.personallinks.com.ar/imagenes/slider/villaVentania.jpg", URLImage.RESIZE_SCALE_TO_FILL);
                   MultiButton mb1 = new MultiButton();
                   mb1.setTextLine1("2");
                   mb1.setIcon(tmp2);
                   Display.getInstance().getCurrent().getContentPane().addComponent(mb1);

                  
                  
                   /** the next way also doesnt work. in the image place only shows an empty box ***************/

                   EncodedImage EI = EncodedImage.createFromImage(EncodedImage.createImage(48, 48), true);
                   Image imageurl =  URLImage.createToStorage(EI, "sj4p2.png", "http://i.stack.imgur.com/sj4p2.png", URLImage.RESIZE_SCALE);

                   MultiButton mb3 = new MultiButton();
                   mb3.setTextLine1("3");
                   mb3.setIcon(imageurl);
                   Display.getInstance().getCurrent().getContentPane().addComponent(mb3);
                   /**-*-*-*-*-*-*-*/
                  
                   /*******************/                           

/*

                   MultiButton mb = new MultiButton();
                   mb.setTextLine1("descripcion");
                   mb.setIcon(tmp);
                    Display.getInstance().getCurrent().getContentPane().addComponent(mb);
*/
                   Display.getInstance().getCurrent().getContentPane().revalidate();
                  // c.getComponentForm().revalidate();
            }
            @Override
            protected void readResponse(InputStream input) throws IOException
            {
//        InfiniteProgress prog = new InfiniteProgress();
 //       Dialog dlg = prog.showInifiniteBlocking();


             
     

               
               
       
                InputStreamReader reader = new InputStreamReader(input);
                JSONParser parser = new JSONParser();
                Hashtable response = parser.parse(reader);
                Vector  registros = (Vector) response.get("root");
               

                /* ---- i tried to load the image into the label , but it didnt work .. why? */   
//                Label myLabel = (Label) findByName("Label",c);
//                ImageDownloadService.createImageToStorage(url, myLabel,"222222", new Dimension(20,20));

                /*---------------------------------------------------------------------------*/
               
                List objLista = (List) findByName("ListItems",c);

                Vector vec = new Vector();
                
                for (int i=0; i<registros.size();i++)
                {
                    Hashtable  registro = (Hashtable)registros.elementAt(i);
                    String descripcion = (String)registro.get("$descripcion");
                    Hashtable h = new Hashtable();
                    h.put("LblCaption",descripcion);

                   
 

                }
       Display.getInstance().getCurrent().getContentPane().repaint();

     

            }
        };

        r.setUrl("http://www.personallinks.com.ar/basedoble/wyn.php");
        r.setPost(false);
        r.addArgument("task", "getNivel");
        r.addArgument("uid", "0");
        InfiniteProgress prog = new InfiniteProgress();
        Dialog dlg = prog.showInifiniteBlocking();
        r.setDisposeOnCompletion(dlg);
        NetworkManager.getInstance().addToQueue(r);
 
  
    }

[CODE]

Shai Almog

unread,
Jun 3, 2015, 12:38:30 AM6/3/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com, rocotoc...@gmail.com
Hi,
if you open the network monitor in the simulator does the request to the jpg go out?
Is the response OK?

rocotoc...@gmail.com

unread,
Jun 3, 2015, 3:10:22 AM6/3/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com
no, but i discover that if i put the following code at the begining of the function, it works, but if i put the same code into the post response, it didnt. can you tell me why and how to fixit??


this way works:

[CODE]
    protected void cargarItemsNivel(int nivel)
    {
        Display.getInstance().getCurrent().getContentPane().removeAll();  
        final String url = "http://www.personallinks.com.ar/imagenes/slider/villaVentania.jpg";
        final String urlImagenesPath = "http://www.personallinks.com.ar/basedoble/imagenes/WYN";

        final Component c =  Display.getInstance().getCurrent();
        final Vector vec = new Vector();

/*   IF I PUT IT HERE , THE IMAGE IS DOWNLOADED */
        MultiButton mb7 = new MultiButton();
        ImageDownloadService.createImageToStorage(urlImagenFullPath, mb7.getIconComponent(),urlImagen,new Dimension(400,400));
        mb7.setTextLine1(descripcion);
        Display.getInstance().getCurrent().getContentPane().addComponent(mb7);


       
        ConnectionRequest r;
        r = new ConnectionRequest()
        {
            @Override
            protected void postResponse()
            {

......
{CODE]

tHIS WAY DIDNT, and i need to get the images into de postResponse>


[CODE]

    protected void cargarItemsNivel(int nivel)
    {
        Display.getInstance().getCurrent().getContentPane().removeAll();  
        final String url = "http://www.personallinks.com.ar/imagenes/slider/villaVentania.jpg";
        final String urlImagenesPath = "http://www.personallinks.com.ar/basedoble/imagenes/WYN";

        final Component c =  Display.getInstance().getCurrent();
        final Vector vec = new Vector();


        ConnectionRequest r;
        r = new ConnectionRequest()
        {
            @Override
            protected void postResponse()
            {

                for (int i=0; i<vec.size();i++)
                {
                    Hashtable  registro = (Hashtable)vec.elementAt(i);
                    String descripcion = (String)registro.get("LblCaption");
                   
                    String urlImagen = (String)registro.get("urlImagen");
                    String urlImagenFullPath = (String)urlImagenesPath +"/"+urlImagen;
                                       
                   
                    MultiButton mb7 = new MultiButton();
                    ImageDownloadService.createImageToStorage(urlImagenFullPath, mb7.getIconComponent(),urlImagen,new Dimension(400,400));
                    mb7.setTextLine1(descripcion);
                    Display.getInstance().getCurrent().getContentPane().addComponent(mb7);
                }
             Display.getInstance().getCurrent().getContentPane().repaint();
            }

[CODE]

Shai Almog

unread,
Jun 3, 2015, 11:11:13 AM6/3/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com, rocotoc...@gmail.com
I'm guessing your connection request failed in some way or got blocked.
Does it appear in the network monitor.

rocotoc...@gmail.com

unread,
Jun 3, 2015, 10:15:14 PM6/3/15
to codenameone...@googlegroups.com, rocotoc...@gmail.com
i find the solution!!

i think that the problem was the following: because in postResponse the connection still active, the diferent ways to download an image can't work.

i fixit, moving the ruotine who loads the images to another place, when the connection who gets the records from my databases is close.

clear?

regards!
Reply all
Reply to author
Forward
0 new messages