3

In my application we have to download around 10 Images from server and display it in mobile. How can I do this? Can I use same HttpConnection for full download? Is any other way for download?

gnat
  • 6,213
  • 108
  • 53
  • 73
Rajesh M P
  • 477
  • 1
  • 10
  • 21
  • in StackOverflow is needed to accept an answer if it has helped you. For doing it see the green tick at left side of the answer. – frayab Feb 07 '12 at 08:13

2 Answers2

3

You can do with this simple loop (Supposing imageList is a List with the url of the images).

HttpConnection  = null;
Image image = null;
for (int i = 0; i < imageList.getSize(); i++) {
    try{
       String urlImage = imageList.get(i);
       hc = (HttpConnection) Connector.open(urlImage);
       image = Image.createImage(hc.openInputStream()));
    } catch (Exception e) {
       e.printStackTrace();
    } finally {
       try {
          hc.close();
       } catch (IOException ex) {
          ex.printStackTrace();
       }
    }
}
frayab
  • 2,512
  • 20
  • 25
  • for Image download loop now we are using TimerTask for download,is it safe or we have to use Thread instead that.using Timer we can easily cancel the task thats why we are using it. – Rajesh M P Feb 07 '12 at 05:51
  • you can use a simple thread, but if you want more control like cancel the task it is better use a timertask – frayab Feb 07 '12 at 08:11
-3

You can try following method in a loop for downloading images from server.

private void downloadImage ( String URL )
{
    try
        {
            System.out.println("URL FOR POST_DATA : "+URL);
            // Open up a http connection with the Web server for both send and receive operations
            httpConnection = (HttpConnection)Connector.open(URL, Connector.READ_WRITE);
            // Set the request method to POST
            httpConnection.setRequestMethod(HttpConnection.POST);
            // Set the request headers 
            httpConnection.setRequestProperty(ConstantCodes.ACTION_MODE_PARAMETER,action);
            httpConnection.setRequestProperty(ConstantCodes.USER_NAME_REQUEST_PARAMETER,userName);
            if(eventName==null || eventName.equals(""))
                eventName="default";
            httpConnection.setRequestProperty(ConstantCodes.EVENT_NAME_REQUEST_PARAMETER, eventName);
            httpConnection.setRequestProperty(ConstantCodes.CAMERAID_REQUEST_PARAMETER, cameraID);
            // all the headers are sending now and connection chanel is establising
            dis = httpConnection.openDataInputStream();

            int ch = 0;
            ByteArrayOutputStream bytearray = new ByteArrayOutputStream(250000);

            while((ch = dis.read()) != -1)
                bytearray.write(ch);

            // fileByte contains whole file in bytes
            byte fileByte[] = bytearray.toByteArray();
            fileSize = fileByte.length;
            System.out.println("Got file size : "+fileSize);
            if(bytearray!=null) bytearray.close();
            midlet.getLastPostedImageResponse(fileByte);
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
            System.out.println("IOException occured during getting last image data : "+ioe.getMessage());
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Eeception occurred during getting last image data : "+e.getMessage());
        }
        finally
        {
            System.out.println("Calling close from Last image posted Action");
            close();
        }
Lucifer
  • 29,392
  • 25
  • 90
  • 143