问题
I'm developing an Android application which uses several images and I would like transfer bigger images (2-6 MB) on a web server to download them when need.
I've never tried it before so I've found a method which uses AsyncTask to download images on button click, is this the best solution?
Any better options or opinions?
EDIT: I'm trying koush's ion
EDIT 2: I tried ion (https://github.com/koush/ion) and I like it here very well, very easy to use. advised
回答1:
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);
回答2:
Best practice: http://developer.android.com/training/displaying-bitmaps/index.html. Useful libraries: Picasso - http://square.github.io/picasso; Glide - github.com/bumptech/glide.
回答3:
I strongly recommend to use Glide or Picasso, which are the most used libraries nowadays. Just google "Glide for Android" or "Picasso for Android" and they take care of the threading, caching, optimization, etc. I hope it helps!
回答4:
Actually the most important reason to useAsyncTask
is that you want a mechanism to do a lengthy operation with out blocking your UI. With it you also get rid of managing child thread and synchronization. And that's it. If you want other functionalities, such as bitmap caching, you will have to implement on your own or use some third-party tools.
回答5:
Use lazy loading images for this and use image caching.This is the best way to do it.
https://github.com/thest1/LazyList
http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134
http://sunil-android.blogspot.in/2013/09/lazy-loading-image-download-from.html
You can find some more examples for lazyloading.Hope the above links are also useful.
来源:https://stackoverflow.com/questions/25499868/best-way-to-download-images-on-android