有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java绘图。createFromStream使用来自远程服务器的映像需要更长时间?

看起来像是可以画的。对于从远程web服务器请求的映像,createFromStream比本地服务器慢。这有意义吗

我有以下代码通过HTTP从安卓应用程序的web服务器获取图像

HttpClient httpclient = new DefaultHttpClient();
HttpParams httpparams = httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(httpparams, CONNECT_TIMEOUT_MS);
HttpConnectionParams.setSoTimeout        (httpparams, CONNECT_TIMEOUT_MS);

// For test only
long time = System.currentTimeMillis();

HttpResponse response = httpclient.execute(new HttpGet(strUrl)); 
StatusLine statusLine = response.getStatusLine();

Log.w("getimage", "Time to get image was " + (System.currentTimeMillis() - time) + "ms");

// Check response
if(statusLine.getStatusCode() == HttpStatus.SC_OK) 
    {
    HttpEntity entity = response.getEntity();
    InputStream inputStream = entity.getContent();

    Log.w("getimage", "This is how many bytes we skipped: " + inputStream.skip(1000000));

    image = Drawable.createFromStream(inputStream, "src name");
}

Log.w("getimage", "Time to return from image call was " + (System.currentTimeMillis()-time) + "ms");

如果我指向我的本地站点(本地网络),第一个计时器日志是30ms(HTTP请求),第二个是66ms(HTTP请求,加上可绘制对象)

如果我做同样的事情,但指向我的web服务器,我会得到298ms(预计会慢一些…),但是对于HTTP请求和drawable对象调用,则为1800ms

这告诉我,可绘制的。createFromStream方法在远程web服务器上花费的时间长了惊人的1.5秒

有什么想法吗?它正在执行任何HTTP调用吗?文档很少。在本地运行时一切都很顺利,但在远程运行时速度非常慢


共 (1) 个答案

  1. # 1 楼答案

    我想是因为在电话里的那段时间让我很难受。对此我无能为力,所以我没有连续地获取每个图像,而是对每个图像使用一个线程(最多5个图像),并并行获取它们

    比如

    m_threadLatch = new CountDownLatch(m_story.photos_thumb.length);
    for(int i=0; i<m_story.photos_thumb.length; i++)
        {
        new HTTPGetImages(((MyApplication)getApplication()).getBaseURL() + m_story.photos_thumb[i]).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
    new AddImagesToUI().execute();
    

    AddImagesToUI等待m_threadLatch,并将图像添加到UI。后果不到一秒钟!感谢上帝