首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下载循环中的图像java me

下载循环中的图像java me
EN

Stack Overflow用户
提问于 2012-02-04 11:52:01
回答 2查看 371关注 0票数 3

在我的应用程序中,我们必须从服务器下载大约10张图片,并将其显示在移动设备上。我该怎么做呢?我可以使用相同的HttpConnection进行完整下载吗?有没有其他的下载方式?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-06 16:31:16

您可以使用这个简单的循环(假设imageList是一个包含图像url的列表)。

代码语言:javascript
复制
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();
       }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2012-02-04 17:31:44

您可以尝试在循环中使用以下方法从服务器下载图片。

代码语言:javascript
复制
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();
        }
票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9138253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档