2013-08-11 2 views
0

Drawable.createFromStream은 원격 웹 서버에서 요청한 이미지보다 로컬 웹 서버에서 요청한 이미지가 느린 것 같습니다. 이게 말이 돼?원격 서버의 이미지로 Drawable.createFromStream이 더 오래 걸립니까?

나는 안드로이드 애플 리케이션을 위해 HTTP를 통해 웹 서버에서 이미지를 가져 오는 다음 코드를 가지고있다.

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 요청)이고, 두 번째는 66 밀리 초 (HTTP 요청 플러스 당김 개체)입니다.

동일한 작업을 수행했지만 웹 서버를 가리키면 298ms (느려질 것으로 예상 됨)가 발생하지만 HTTP 요청 및 drawable 객체 호출은 1800ms가됩니다.

이것은 Drawable.createFromStream 메서드가 원격 웹 서버에서 1.5 초 더 오래 걸리고 있음을 알려줍니다.

이것에 대한 아이디어가 있습니까? HTTP 호출을 수행하고 있습니까? 문서가 희박합니다. 로컬에서 실행할 때 그레이비가 전부 였지만 리모컨으로 갈 때는 정말 느려졌습니다. ...

+0

어떤 종류의 InputStream HttpClient가 반환되는지 잘 모르겠지만 ResponseStream을 BufferedInputStream에서 래핑 해 보았습니까? –

+0

제안 해 주셔서 감사합니다. 불행히도 많은 차이를 만드는 것 같지 않습니다. 그것은 createFromStream 호출이 전선을 통해 무언가를하고있는 것과 같습니다 ... – arons

+0

그러면 입력을 모두 한 번에 읽는 것이 좋습니다. 그런 다음 Drawable에 전달합니다. 일부 예는 http://javarevisited.blogspot.com/2012/08/convert-inputstream-to-string-java-example-tutorial.html을 참조하십시오. –

답변

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에 이미지를 추가합니다. 결과? 1 초도 안돼! 평행선에 대한 하나님 감사합니다!

관련 문제