如何使用客户端中的URL从服务器下载多个图像

时间:2013-08-09 12:29:57

标签: android json android-asynctask

我正在使用本教程: http://manishkpr.webheavens.com/android-viewpager-as-image-slide-gallery-swipe-gallery/

这里他使用了可绘制的图像,但现在我想使用服务器图像

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);
}
}

我仍在使用像这样的图像路径:

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

2 个答案:

答案 0 :(得分:5)

您无需使用引用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.javaFileCache.javaMemoryCache.javaUtils.java

然后在适配器类中执行以下操作。

@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 );
}

答案 1 :(得分:1)