[英]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.我正在编写一个应用程序,我需要在其中显示来自 URL 的图像。 Now suppose I have to show 100 images, one after the another and each photo needs to be downloaded from the internet.
现在假设我要展示 100 张图片,一张一张,每张图片都需要从互联网上下载。
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.我正在使用图库视图来显示图像,但问题是当我将 URL 传递给
getView
function 时,它开始下载所有 100 张图像(仅在异步任务中是),但下载 100 张图像会减慢系统速度。 What I want to achieve is, when i move right, my program should pick the url and download it from the internet.我想要实现的是,当我向右移动时,我的程序应该选择 url 并从互联网上下载。
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现在的问题是,一旦为
getview
中存在的所有 URL 加载活动,就会调用GalleyList
,这会减慢系统速度
@cyngus: i was trying to use your answer.. but now sure how to use this.. @cyngus:我试图使用您的答案..但现在确定如何使用它..
I created the executor object as suggested by you我按照您的建议创建了执行程序 object
Executor e = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE,
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
But im not able to find out how to use this for calling my Async Task(BitmapDownloaderTask)但我无法找出如何使用它来调用我的异步任务(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> {
}
The default Executor
for AsyncTask is a ThreadPoolExecutor
that uses a LinkedBlockingQueue
that is capacity restricted to 10 items. AsyncTask 的默认
Executor
是ThreadPoolExecutor
,它使用容量限制为 10 个项目的LinkedBlockingQueue
。 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.这意味着一旦队列未处理队列达到 10 个项目,
Executor
就会开始添加线程,因为它无法向工作队列添加更多项目。 If you want to prevent this, you could create your own 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>());
Then you can call AsyncTask.executeOnExecutor()
.然后你可以调用
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.这将使用一个无界队列,这可能不是您想要的,也不是所有可能用例的最佳选择,但它应该将工作线程的数量保持在 5 并限制您看到的减速。
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.
查看链接Lazy load of images in ListView这在本示例中为 listview 完成,但也可以使用 gridview 实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.