2013-02-25 3 views
1

for 루프를 사용하여 doInBackground의 배열 목록에서 이미지 URL을 읽는 중이지만 루프가 증가하지 않습니다. 첫 번째 URL이 이미지를로드하고 저장하는 중입니다. 여기에 코드입니다 :비동기 작업의 Doinbackground에서 루프가 증가하지 않음

class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> { 
    // This class definition states that DownloadImageTask will take String 
    // parameters, publish Integer progress updates, and return a Bitmap 
    @SuppressWarnings("unused") 
    protected Bitmap doInBackground(Void... paths) { 
     //URL url; 
     try { 
       for(int j=0; j<List.size();j++) 
      { 
        reviewImageLink =List.get(j).get(TAG_Image); 
        URL  url = new URL(reviewImageLink); 
        // URL reviewImageURL; 
        String name = reviewImageLink.substring(reviewImageLink .lastIndexOf("/") + 1,reviewImageLink.length()); 
         //try { 

          if (!hasExternalStoragePublicPicture(name)) { 
           isImage = false; 
           //new DownloadImageTask().execute(); 
           Log.v("log_tag", "if"); 
           isImage = true; 
           File sdImageMainDirectory = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.directory)); 
          //if(!sdImageMainDirectory.exists()){ 
           sdImageMainDirectory.mkdirs(); 
           File file = new File(sdImageMainDirectory, name); 
           Log.v("log_tag", "Directory created");} 
        //} 
        // catch (MalformedURLException e) { 
         // Log.v(TAG, e.toString()); } 
       // } 

      // }//try 
      // catch (Exception e) { 
       //  e.printStackTrace();} 
      //url = new URL(List.get(j).get(TAG_Image)); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      int length = connection.getContentLength(); 
      InputStream is = (InputStream) url.getContent(); 
      byte[] imageData = new byte[length]; 
      int buffersize = (int) Math.ceil(length/(double) 100); 
      int downloaded = 0; 
      int read; 
      while (downloaded < length) { 
       if (length < buffersize) { 
        read = is.read(imageData, downloaded, length); 
       } else if ((length - downloaded) <= buffersize) { 
        read = is.read(imageData, downloaded, length- downloaded); 
       } else { 
        read = is.read(imageData, downloaded, buffersize); 
       } 
       downloaded += read; 
       setProgress((downloaded * 100)/length); 
      } 
      Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,length); 
      if (bitmap != null) { 
       Log.i(TAG, "Bitmap created"); 
      } else { 
       Log.i(TAG, "Bitmap not created"); 
      } 
      is.close(); 
      return bitmap; 

     } 
     }catch (MalformedURLException e) { 
      Log.e(TAG, "Malformed exception: " + e.toString()); 
     } catch (IOException e) { 
      Log.e(TAG, "IOException: " + e.toString()); 
     } catch (Exception e) { 
      Log.e(TAG, "Exception: " + e.toString()); 
     } 
     return null; 

    } 

    protected void onPostExecute(Bitmap result) { 
     String name = reviewImageLink.substring(reviewImageLink.lastIndexOf("/") + 1,reviewImageLink.length()); 
     if (result != null) { 
      hasExternalStoragePublicPicture(name); 
      saveToSDCard(result, name); 
      isImage = true; 

     } else { 
      isImage = false; 

     } 
    } 
} 
+0

나는'return bitmap;이라고 생각한다. loop for breaking –

+0

어떻게 든 List.size()가 이상하다고 생각하는데, List가 클래스가 아니라 객체가 아닌가? – Gjordis

+0

@ ρяσѕρєяK 예, 답장 비트 맵을 말할 때; 문 루프가 잘 실행되지만 어디서 돌아올 수 있습니까? –

답변

1

List는 인터페이스, 당신은 ArrayList에 또는 뭔가있을 같은 List.size() .Define 한 목록의 하위 유형처럼 사용할 수있는 것은 loop.For 전직에 대한 아무 잘못 없다 :

List<Integer> li = new ArrayList<Integer>(); 
li.add(10); 
li.add(20)'; 
for(int i = 0; i <= li.size(); ++i) { 
//your stuff 
}