对于从远程Web服务器请求的映像与本地映像相比,Drawable.createFromStream似乎较慢。这有意义吗?
我有以下代码通过HTTP为Android应用程序从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");
如果我指向我的本地站点(本地网络),第一个计时器日志是30毫秒(HTTP请求),第二个是66毫秒(HTTP请求,加上可绘制对象)。
如果我做同样的事情,但指向我的网络服务器,我得到298ms(预计它会更慢......),然后是180ms用于HTTP请求和可绘制对象调用。
这告诉我Drawable.createFromStream方法在远程Web服务器上花费的时间长了1.5秒?
有关于此的任何想法吗?它是否正在执行任何HTTP调用?文档很稀疏。在本地跑步时一切都很好吃,但是在远程跑步时变得很慢......
答案 0 :(得分: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。结果?不到一秒钟!感谢上帝的并行!