Reputation: 179
I'm working on a music player app for android and am trying to find the best way of dealing with images. I'm loading artist images from the web, and album images either from the MediaStore or the web depending on the circumstances. I will need to add either set of images (artist or album) to listViews at different times.
Storing the images in memory (HashTable) would make loading very quick, but could become impractical if the user has a very large music library.
However I can't load them on an "as needed" basis because I need to bind them to a listView. In addition, I have a notification that displays the artist image and provides navigation controls. If I wait to load the image when the user clicks the next button, it might not load before the notification view updates.
Is the best approach to connect to the Net on first load and then save the files locally, and then load them as needed? If I do it this way, will the image be loaded quickly enough to keep up with a user fast-clicking the next button and refreshing the artist image view?
Thanks,
Upvotes: 2
Views: 3109
Reputation: 133560
Use a listview or gridview to display images.
You can use https://github.com/nostra13/Android-Universal-Image-Loader Universal Image Loader to load images either form MediaStore or from the web. Uses caching. Based on Lazy Loading but has more configuration options.
In you adapter contructor
File cacheDir = StorageUtils.getOwnCacheDirectory(a, "UniversalImageLoader/Cache");
// Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
// You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.enableLogging()
.build();
// Initialize ImageLoader with created configuration. Do it once.
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_id)//dummy image
.cacheInMemory()
.cacheOnDisc()
.displayer(new RoundedBitmapDisplayer(20))
.build();
In your getView()
ImageView image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(imageurl, image,options);
You can also use https://github.com/thest1/LazyList. Also uses caching.
Also use a ViewHolder in listview or grdiview. http://developer.android.com/training/improving-layouts/smooth-scrolling.html.
Upvotes: 1
Reputation: 12656
Load your images from local file storage or MediaStore
in the getView()
function of your ListView's
ListAdapter
or SimpleListAdapter
. The adapter will take care of only calling getView()
when necessary for the item(s) needed by position
. Your system will be fast enough to load the images in this manner when they are needed.
You will have to fetch all images from the web at an earlier time and store them in the local filesystem so they are available for display by the ListView
.
Upvotes: 0