2013-04-03 3 views
1

나는 게임을 만들기 시작했다.이 게임은 서버에서 이미지를 얻는다.URL에서 이미지

비트 맵을 사용하여 이미지 * S * 및 그 작품을 천천히 변환합니다.

22 이미지 (각 이미지 당 100KB)를로드하는 데 25-40 초가 걸립니다.


public static Bitmap getBitmapFromURL(String src) { 
    try { 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

구현 :


Bitmap pictureBitmap = ImageFromUrl.getBitmapFromURL(path); 

PS ..

는 내가 전에 LazyList을 사용하고, 내 목표를 위해 아니다.

다른 혜택이 더 있습니까? BitmatpFactory 공장은 항상 데이터를 수집하기 위해 입력 스트림 기다려야했다 그래서 그것의, BitmatpFactory를 사용하여 디코딩 동안

TNX ....

+0

http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by- url-in-android –

답변

1

당신은 HTTP 연결에서 getInputStream()에 노력하고 있습니다.

그리고 추가 오류가 발생할 수있는 tin finally 블록이 예상되는 입력 스트림 중 close()이 표시되지 않습니다.

  • 가 분리 된 스레드에서 HTTP 연결을 만들고, 그래서 당신은 동시에 이미지를 다운로드 할 수 있습니다

    이보십시오.

  • 파일을 다운로드 한 후에 만 ​​디코드 비트 맵을 만듭니다 (비트 맵 디코더 용으로 다른 스트림을 열어야 할 수도 있지만 현재 솔루션은 훨씬 빠르고 명확합니다).

또한 연결 대역폭을 점검하여 수행중인 작업이이 요소 (네트워크 대역폭)에 의해 제한되는지 확인하십시오.

[업데이트]이 일부 폴더의 유틸리티 기능은 다음과 같습니다이 링크를 참조 할 수 있습니다

/** 
* Util to download data from an Url and save into a file 
* @param url 
* @param outFilePath 
*/ 
public static void HttpDownloadFromUrl(final String url, final String outFilePath) 
{ 
    try 
    { 
     HttpURLConnection connection = (HttpURLConnection) (new URL(url)).openConnection(); 
     connection.setRequestMethod("GET"); 
     connection.setDoOutput(true); 
     connection.connect(); 

     FileOutputStream outFile = new FileOutputStream(outFilePath, false); 
     InputStream in = connection.getInputStream(); 

     byte[] buffer = new byte[1024]; 
     int len = 0; 
     while ((len = in.read(buffer)) > 0) 
     { 
      outFile.write(buffer, 0, len); 
     } 
     outFile.close(); 
    } 
    catch (MalformedURLException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 

/** 
* Spawn a thread to download from an url and save into a file 
* @param url 
* @param outFilePath 
* @return 
*  The created thread, which is already started. may use to control the downloading thread. 
*/ 
public static Thread HttpDownloadThreadStart(final String url, final String outFilePath) 
{ 
    Thread clientThread = new Thread(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      HttpDownloadFromUrl(url, outFilePath); 
     } 
    }); 
    clientThread.start(); 

    return clientThread; 
}