2011-07-03 6 views
3

URL의 이미지를 표시해야하는 애플리케이션을 작성 중입니다. 이제 내가 100 개의 이미지를 보여줘야한다고 가정 해 보겠습니다. 하나씩 그리고 각각의 사진을 인터넷에서 다운로드해야합니다.안드로이드가 갤러리에서 이미지를 인터넷에서 다운로드해야합니다.

갤러리보기를 사용하여 이미지를 표시하지만 문제는 URL을 getView으로 전달하면 100 개의 이미지를 모두 다운로드하기 시작합니다 (비동기 작업에서만 가능함). 그러나 100 개의 이미지를 다운로드하면 시스템 속도가 느려집니다. . 제가 성취하고자하는 것은, 제가 올바르게 움직일 때, 제 프로그램은 URL을 골라 인터넷에서 다운로드해야합니다. 내가 사용하려고 시도했다 : 지금 여기에 문제가 즉시 활동이 시스템 아래


@cyngus를 느리게 GalleyList에 존재하는 모든 URL에 대한로드로 getview라고한다

public class ViewImage extends Activity { 

private String albumID; 
private ArrayList<MediaBO>galleryList; 
private Context context; 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.localgallery); 

    this.context = this; 
    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 
    albumID = extras.getString("albumID"); 
    int position = extras.getInt("position"); //position of the image selected 

    DataBaseOperation dataBaseOperation = new DataBaseOperation(this); 
    galleryList = dataBaseOperation.queryAllPhoto(albumID); //this returns the list of Photos needs to be shown.it has the URL of the photos 

    Gallery g = (Gallery) findViewById(R.id.localGallery); 
    g.setAdapter(new ImageAdapter(this)); 
    g.setSelection(position); 

} 
public class ImageAdapter extends BaseAdapter{ 
    private Context mContext; 
    int mGalleryItemBackground; 
    public ImageAdapter(Context c) { 
     mContext = c; 
    } 
    public int getCount(){ 
     return galleryList.size(); 
    } 
    public Object getItem(int position){ 
     return position; 
    } 
    public long getItemId(int position) { 
     return position; 
    } 
    public View getView(int position, View convertView, ViewGroup parent){ 

     ImageView i = new ImageView(mContext); 

     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.localgallery, null); 
     } 
     String url = galleryList.get(position).getUrl(); 

     DOWNLOAD()//download the photo using Async Task 
     v = i; 
     return v; 
    } 

} 
    } 

Executor e = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE, 
      TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); 
당신에 의해 제안 답 ..하지만 지금은 이것을 사용하는 방법을 잘 ..

나는 집행자 객체를 생성

그러나 호출이를 사용하는 방법을 찾을 수 없습니다 메신저 내 비동기 작업 (BitmapDownloaderTask)

BitmapDownloaderTask task = new BitmapDownloaderTask(con,showDialog,i,path); 
    task.execute(url,null,null); 

내 Asyn 작업이

private class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> { 
    } 
+1

인터넷에서 이미지를 다운로드하고 진행률 막대를 사용하여 gridview에 추가하려면 여기 URL을 따르십시오. http://stackoverflow.com/questions/13415804/android-progress-not-working-accordingly/13416436#comment18333882_13416436 – sai

답변

0

AsyncTask를 기본 Executor이다 사용하는 ThreadPoolExecutor입니다 LinkedBlockingQueue은 용량이 10 개로 제한되어 있습니다. 즉, 큐 처리되지 않은 큐가 10 개의 항목에 도달하면 Executor은 작업 큐에 항목을 더 추가 할 수 없기 때문에 스레드 추가를 시작합니다. 이를 방지하려면 직접 Executor을 생성 할 수 있습니다.

// same values as the default implementation in AsyncTask 
int CORE_POOL_SIZE = 5; 
int MAX_POOL_SIZE = 128; 
int KEEP_ALIVE = 1; 

Executor e = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE, 
     TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); 

그런 다음 AsyncTask.executeOnExecutor()으로 전화 할 수 있습니다. 이렇게하면 바인드되지 않은 대기열이 사용됩니다(). 사용 가능한 모든 유스 케이스에 대해 최선이 아니거나 최적 인 경우가 있지만 작업자 스레드 수를 5로 유지하고 표시되는 속도를 제한해야합니다.

0

지연로드가 문제를 해결할 수 있다고 생각합니다. Lazy load of images in ListView이 예제에서는 listview에 대해 수행되었지만 gridview에도 적용 할 수 있습니다.

관련 문제