[英]Best way to download images on Android
我正在开发一个使用多个图像的Android应用程序,我想在Web服务器上传输较大的图像(2-6 MB)以在需要时下载它们。
我之前从未尝试过,所以我找到了一种使用AsyncTask在单击按钮时下载图像的方法,这是最好的解决方案吗?
还有更好的选择或意见吗?
编辑:我正在尝试库什的离子
编辑2:我尝试过ion( https://github.com/koush/ion ),在这里我非常喜欢它,非常易于使用。 建议
Use Universal image loader for downloading images asynchronously.
[https://github.com/nostra13/Android-Universal-Image-Loader][1].
The Library itself has a sample code to download image.you may refer it..
[1]: https://github.com/nostra13/Android-Universal-Image-Loader
After downloading library add library with your project and insert the below code at necessary place
String final_url="www.google.com/.....";
ImageView image;
ImageLoader imageloader = ImageLoader.getInstance();
imageloader.init(ImageLoaderConfiguration.createDefault(context));
DisplayImageOptions options; = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.resetViewBeforeLoading(true).cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565).considerExifParams(true)
.cacheInMemory(true)
.displayer(new FadeInBitmapDisplayer(300)).build();
imageloader.displayImage(final_url, image);
最佳做法: http : //developer.android.com/training/displaying-bitmaps/index.html 。 有用的库:Picasso- http ://square.github.io/picasso; Glide-github.com/bumptech/glide。
我强烈建议使用Glide或Picasso,它们是当今最常用的库。 只是谷歌“ Glide for Android”或“ Picasso for Android”,他们负责线程,缓存,优化等工作,希望对您有所帮助!
实际上,使用AsyncTask
的最重要原因是您希望一种机制进行长时间的操作而不会阻塞UI。 使用它,您还可以摆脱管理子线程和同步的麻烦。 就是这样。 如果需要其他功能(例如位图缓存),则必须自己实现或使用某些第三方工具。
为此使用延迟加载图像并使用图像缓存。这是最好的方法。
https://github.com/thest1/LazyList
http://sunil-android.blogspot.in/2013/09/lazy-loading-image-download-from.html
您可以找到更多有关延迟加载的示例。希望以上链接也很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.