在画廊的Android显示图像,需要从互联网上下载 [英] Android showing Image in Gallery which needs to be downloaded from Internet
问题描述
我写的,我需要从显示的URL图像的应用程序。现在假设我有100显示图像,一前一后另一个和每张照片需要从互联网上下载。
我使用的图库视图来显示图像,但问题是当我通过URL来 getView
功能将开始下载所有的100张图像(是在异步唯一的任务),但下载100幅图像减慢系统。我想实现的是,当我向右移动,我的程序应该选择的网址,并从互联网上下载它。
公共类ViewImage延伸活动{私人字符串ALBUMID;
私人的ArrayList< MediaBO> galleryList;
私人上下文的背景下;
公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);
的setContentView(R.layout.localgallery); this.context =这一点;
意向意图= getIntent();
捆绑额外= intent.getExtras();
ALBUMID = extras.getString(ALBUMID);
INT位置= extras.getInt(位置); //选择的图像的位置 DataBaseOperation dataBaseOperation =新DataBaseOperation(本);
galleryList = dataBaseOperation.queryAllPhoto(ALBUMID); //这个返回图片列表必须shown.it有照片的网址 画廊G =(图库论坛)findViewById(R.id.localGallery);
g.setAdapter(新ImageAdapter(本));
g.setSelection(位置);}
公共类ImageAdapter延伸BaseAdapter {
私人语境mContext;
INT mGalleryItemBackground;
公共ImageAdapter(上下文C){
mContext = C;
}
公众诠释的getCount(){
返回galleryList.size();
}
公共对象的getItem(INT位置){
返回的位置;
}
众长getItemId(INT位置){
返回的位置;
}
公共查看getView(INT位置,查看convertView,父母的ViewGroup){ ImageView的I =新ImageView的(mContext); 视图V = convertView;
如果(V == NULL){
LayoutInflater VI =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
V = vi.inflate(R.layout.localgallery,NULL);
}
字符串URL = galleryList.get(位置).getUrl(); 下载()//下载使用异步任务照片
V = I;
返回伏;
}}
}
现在这里的问题是 getview
是一旦活动被装载在所有的URL present叫 GalleyList
这会减慢系统速度
@cyngus:我试图用你的答案..但现在知道如何使用这个..
我创建的执行人对象由您的建议
执行人E =新的ThreadPoolExecutor(CORE_POOL_SIZE,MAX_POOL_SIZE,KEEP_ALIVE,
TimeUnit.SECONDS,新的LinkedBlockingQueue<&的Runnable GT;());
但我不是能够找出如何使用这个调用我的异步任务(BitmapDownloaderTask)
BitmapDownloaderTask任务=新BitmapDownloaderTask(CON,的ShowDialog我,路径);
task.execute(URL,NULL,NULL);
我ASYN的任务是
私有类BitmapDownloaderTask扩展的AsyncTask<弦乐,太虚,位图> {
}
我想懒加载可以解决你的问题。看看链接<一个href=\"http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview\">Android - 我该如何进行图像的延迟加载ListView中这为列表视图在这个例子中完成,但可与GridView控件也可以implmented。
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> {
}
I guess lazy loading can solve your problem. Have a look at the link Android - How do I do a lazy load of images in ListView This is done for listview in this example but can be implmented with gridview also.
这篇关于在画廊的Android显示图像,需要从互联网上下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!