从Server上在Android客户端上加载图像的最佳方法

时间:2013-12-31 06:23:34

标签: java android image web-services

我们是否应该使用URL直接在Android客户端上加载图像 要么 我们是通过服务器端APP /或webservice获取图像(传递图像名称附加到URL)。 哪种方式更好。

3 个答案:

答案 0 :(得分:2)

为每个图像下载大图像然后下采样将是非常低效的。首先,下载大小比需要的大。其次,您需要将其加载到内存中以执行下采样,第三次下采样有点慢。

您可以使用web api返回缩略图网址列表和完整图片网址,您可以在屏幕上显示视图,并使用缓存让它们保持一段时间。确保你也缩小了缩略图的大小。然后,当用户点击图像时,我会下载完整的图像,然后在它到达时在背景上下载,然后再显示它。

您需要实现LazyLoading是从url异步下载图像的最佳方式。

查看tutorial

您需要阅读documentation before proceeding to working with images.

答案 1 :(得分:1)

您可以使用ImageLoader

从服务器直接网址加载图片

首先从net获取universal-image-loader-1.8.6-with-sources.jar并在lib中设置构建路径。

//------ImageLoader from lib.    ------for load images-----------------------
protected ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options;
//----------------------------------------------------------------

//启动imageLoader ---------------

imageLoader.init(ImageLoaderConfiguration.createDefault(CON));

// ---------------------

//初始化选项.........用于图像.....................

    options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.stub_image)
    .showImageForEmptyUri(R.drawable.image_for_empty_url)
    .cacheInMemory()
    .cacheOnDisc()
    .bitmapConfig(Bitmap.Config.RGB_565)
    .build();

    //------------------------------------------------------    

//传递像--->的网址http://www.xyz.com/xyz/upload/image//2013/08/30/1377865578220.jpg // ---传递持有者,并显示图像(如果存在) // Constant.Image_URL + List.get(position).get(“photo”)< ---带图像名称的完整URL //holder.img是您不会设置图像的图像视图

imageLoader.displayImage(Constant.Image_URL+List.get(position).get("photo"), holder.img, options);

//使用这些代码从url

获取图像

答案 2 :(得分:0)

如果您没有使用高分辨率位图,那么它非常适合您。

http://howrobotswork.wordpress.com/2013/06/02/downloading-a-bitmap-asynchronously-with-volley-example/

相关问题