Skip to content Skip to sidebar Skip to footer

Android Showing Image In Gallery Which Needs To Be Downloaded From Internet

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 th

Solution 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 AsyncTaskintCORE_POOL_SIZE=5;
intMAX_POOL_SIZE=128;
intKEEP_ALIVE=1;

Executore=newThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE,
        TimeUnit.SECONDS, newLinkedBlockingQueue<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.

Solution 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.

Post a Comment for "Android Showing Image In Gallery Which Needs To Be Downloaded From Internet"