问题
I am writing an application in which I need to show images from the URLs. Now suppose I have to show 100 images, one after the another and each photo needs to be downloaded from the internet.
I am using the Gallery View to show the images, but the problem is when I pass the URL to getView
function it starts downloading all the 100 images (yes in Async Task only), but downloading 100 images slows down the system. What I want to achieve is, when i move right, my program should pick the url and download it from the internet.
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;
}
}
}
Now the problem here is getview
is called as soon as the activity is loaded for all the URL present in the GalleyList
which slows down the system
@cyngus: i was trying to use your answer.. but now sure how to use this..
I created the executor object as suggested by you
Executor e = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE,
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
But i m not able to find out how to use this for calling my Async Task(BitmapDownloaderTask)
BitmapDownloaderTask task = new BitmapDownloaderTask(con,showDialog,i,path);
task.execute(url,null,null);
My Asyn Task is
private class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
}
回答1:
The default Executor
for AsyncTask is a ThreadPoolExecutor
that uses a LinkedBlockingQueue
that is capacity restricted to 10 items. This means that once the queue unhandled queue reaches 10 items, the Executor
starts adding Threads since it can't add more items to the work queue. If you want to prevent this, you could create your own 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>());
Then you can call AsyncTask.executeOnExecutor()
. This will use an unbounded queue, which is possibly not exactly what you want or the best for all possible use cases, but it should keep the number of worker threads at 5 and limit the slowdown you see.
回答2:
I guess lazy loading can solve your problem. Have a look at the link Lazy load of images in ListView This is done for listview in this example but can be implmented with gridview also.
来源:https://stackoverflow.com/questions/6561202/android-showing-image-in-gallery-which-needs-to-be-downloaded-from-internet