2016-09-17 27 views
0

我想下載給定一個url的多個圖像。我有以下代碼卡住了。有人可以幫我把折舊的http轉換成URL嗎?android下載多個圖像(使用URL連接)

  for (int i = 0; i < URLS.length; i++) { 
       try { 
        File firstFile = new File(directory + "/" + i + ".png"); 
        if (firstFile.exists()==false) 
        { 
         HttpClient httpClient = new DefaultHttpClient(); 
         HttpGet httpGet = new HttpGet(URLS[i]); 
         HttpResponse resp = httpClient.execute(httpGet); 

         if(resp.getStatusLine().getStatusCode()==200){ 
          HttpEntity entity = resp.getEntity(); 
InputStream is = entity.getContent(); 

          Boolean status = firstFile.createNewFile(); 

          FileOutputStream fouts = new FileOutputStream(firstFile); 
          byte[] buffer = new byte[1024]; 
          long total = 0 ; 
          int count; 

          while ((count = is.read(buffer))!=-1){ 
           total+=count; 
           fouts.write(buffer,0,count); 
          } 
          fouts.close(); 
          is.close(); 
          publishProgress(i); 
         } 
        } 
       } 
       catch(MalformedURLException e){e.printStackTrace();} 

       catch(IOException e){e.printStackTrace();} 
      } 
      return null; 
     } 
     protected void onProgressUpdate(Object... values){ 
      super.onProgressUpdate(values); 
     } 
    } 
} 
+0

解釋你的問題..什麼是不準確.. –

+0

那麼,你的問題是什麼?你想要達到什麼目標,哪些不起作用?如果您編輯了代碼,這也會更好:只留下與問題相關的部分並修復格式。 –

+0

對不起模糊的問題描述。每次運行時,我都需要幫助,以避免出現錯誤。 HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(URLS [i]); HttpResponse resp = httpClient.execute(httpGet);如果(resp.getStatusLine()。getStatusCode()== 200){ HttpEntity entity = resp.getEntity(); InputStream is = entity.getContent(); –

回答

0

下載圖像可以使用滑翔或畢加索庫進行簡單的,這是更快的庫下載圖像。 Check this tutorial