如何从服务器在客户端使用的URL下载多张图片 [英] How to download multiple images from Server using URL in Client Side
问题描述
我使用本教程:
的http://manishkpr.webheavens.com/android-viewpager-as-image-slide-gallery-swipe-gallery/
在这里他已经习惯了图像绘制,但现在我想使用服务器映像
ImageAdapter.java:
公共类ImageAdapter扩展PagerAdapter {
上下文语境;
私人诠释[] = GalImages新INT [] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
ImageAdapter(上下文的背景下){
this.context =背景;
}
@覆盖
公众诠释的getCount(){
返回GalImages.length;
}@覆盖
公共布尔isViewFromObject(查看视图,Object对象){
返回查看==((ImageView的)对象);
}@覆盖
公共对象instantiateItem(ViewGroup中的容器,INT位置){
ImageView的ImageView的=新ImageView的(上下文);
INT填充= context.getResources()getDimensionPixelSize(R.dimen.padding_medium)。
imageView.setPadding(填充,填充,填充,填充);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages [位置]);
((ViewPager)容器).addView(ImageView的,0);
返回ImageView的;
}@覆盖
公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象){
((ViewPager)容器).removeView((ImageView的)对象);
}
}
不过我用图片路径:
R.drawable.one,
R.drawable.two,
R.drawable.three
但现在我想用图片的路径如下图所示:
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo1.png,
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo2.png,
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo3.png
您不必使用引用的 http://www.androidhive.info/2012/07/android-loading-image-from-url-http/ 。
只要创建的字符串,其中包含的URL为
阵列 的String [] = imagUrl {
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo1.png,
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo2.png,
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo3.png};
在创建阵列后只复制文件 ImageLoader.java
, FileCache.java
, MemoryCache.java
和 Utils.java
在应用程序中。
和之后,在你的适配器类做如下。
@覆盖
公共对象instantiateItem(ViewGroup中的容器,INT位置){
// ImageView的显示
ImageView的ImageView的=新ImageView的(上下文);
// ImageLoader的类实例
ImageLoader的imgLoader =新ImageLoader的(getApplicationContext());
//装载机形象 - 将之前加载图片显示
INT装载机= R.drawable.loader;
//每当要加载从URL图像
//调用函数DisplayImage
// URL - 图像的URL加载
//装载机 - 装载机的图像,将图像获得前将显示
//形象 - 的ImageView
imgLoader.DisplayImage(imagUrl [位置],装载机,ImageView的);
}
I am using this tutorial: http://manishkpr.webheavens.com/android-viewpager-as-image-slide-gallery-swipe-gallery/
here he has used drawable images, but now i want to use Server Images
ImageAdapter.java:
public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return GalImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
Still i am using Images path like this:
R.drawable.one,
R.drawable.two,
R.drawable.three
But now i want to use Images path like below:
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo1.png,
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo2.png,
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo3.png
You do not have to do anything using the reference http://www.androidhive.info/2012/07/android-loading-image-from-url-http/ .
Just create the Array of string which contains the urls as
String[] imagUrl={
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo1.png,
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo2.png,
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo3.png };
After creating array just copy the files ImageLoader.java
,FileCache.java
,MemoryCache.java
and Utils.java
in your application.
And after that in your adapter class do as following.
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Imageview to show
ImageView imageView = new ImageView(context);
// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
// Loader image - will be shown before loading image
int loader = R.drawable.loader;
// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView
imgLoader.DisplayImage(imagUrl[position], loader, imageView );
}
这篇关于如何从服务器在客户端使用的URL下载多张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!