2016-10-26 4 views
2

링크가있는 String []에서 Bitmap []을 가져오고 싶습니다. 그러나 이것은 내가 원하는대로 작동하지 않습니다. 나는이 방법 :안드로이드는 Picasso에서 비트 맵 배열로 이미지를 가져옵니다.

onSuccess 함수 후 목록에 비트 맵을 추가하기 때문에 문제는 null 배열을 얻습니다. 어떻게하면 모든 onSuccess가 비트 맵을 추가 한 다음 다시 돌아올 때까지 기다릴 수 있습니까?

답변

4

피카소의 get() 기능은 사용자가 원하는 기능을 수행합니다. ImageView에 이미지를로드하는 대신 비트 맵을 다운로드합니다. Picasa의 get() 메서드는 주 스레드에서 호출 할 수 없습니다. 내 예제에서는 AsyncTask를 사용하여 별도의 스레드에서 이미지를 다운로드합니다.

String[] images = new String[] {"http://path.to.image1.jpg", "http://path.to.image2.jpg"}; 
    new AsyncTask<String[], Void, List<Bitmap>>() { 
     @Override 
     protected List<Bitmap> doInBackground(String[]... params) { 
      try { 
       List<Bitmap> bitmaps = new ArrayList<Bitmap>(); 
       for (int i = 0; i < params[0].length; ++i) { 
        bitmaps.add(Picasso.with(getActivity()).load(params[0][i]).get()); 
       } 
       return bitmaps; 
      } catch (IOException e) { 
       return null; 
      } 
     } 

     @Override 
     public void onPostExecute(List<Bitmap> bitmaps) { 
      if (bitmaps != null) { 
       // Do stuff with your loaded bitmaps 
      } 
     } 
    }.execute(images); 
1

integer가 성공할 때마다 정수가 images.lengh()와 같아 질 때까지 늘릴 수 있습니다. 이것을 루프로 점검 할 수 있습니다. 그리고 루프 내에서 return 내의 if 절이 있습니다. 루프에서

int currentSuccess = 0; 

예를

를 들어

:

 @Override 
      public void onSuccess() { 
       temp.add(BitmapRes.drawableToBitmap(img.getDrawable())); 
       movableBackgroundContainer.removeView(img); 
       currentSuccess++; 
      } 

및 반환 : 도움이

while(true){ 
    if(currentSuccess == images.length){ 
     return temp.toArray(new Bitmap[temp.size()]); 
    } 
} 

희망.

관련 문제