i am trying to implement an application which load bunch of images (Like Google images loading ) from server.by using which concept i can implement this,i tried google didn't find a solution give me good suggestions and any examples or links thank you in advance.
Use a listview or gridview to display images.
Inflate your custom layout and display images in a listview or gridview.
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.
For Endless list you can use commonware endless list. https://github.com/commonsguy/cwac-endless.
Related
I started to use Universal Image Loader instead of Picasso, to avoid the image-not-loading problem. Now it emerges in Universal Image Loader too. It is the same problem with the same unique image, but not other images.
The problematic Image:
http://luxproperty.kaytami.com/platform/media/image/jpeg/2015/01/4_4.jpg
Other properly-behaved images:
http://luxproperty.kaytami.com/platform/media/image/jpeg/2015/01/4.2_4.jpg
http://luxproperty.kaytami.com/platform/media/image/jpeg/2015/01/4.3_4.jpg
http://luxproperty.kaytami.com/platform/media/image/jpeg/2015/01/4.4_4.jpg
They are of similar sizes. And about my Universal Image Loader, the setting is as such:
In Application:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
.diskCacheSize(50 * 1024 * 1024) // 50 Mb
.tasksProcessingOrder(QueueProcessingType.LIFO)
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
In activity:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.image_placeholder)
.showImageForEmptyUri(R.drawable.image_error)
.showImageOnFail(R.drawable.image_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.build();
.......
ImageLoader.getInstance().displayImage(url), image, options);
Any hints would be much appreciated. I just cannot get rid of this problem anyhow, neither with Picasso nor Universal Image Loader.....
My previous StackOverFlow with Picasso:
Picasso cannot load images for some URL (no special characters)
It seems like the image is corrupted as downloading and opening it in photoshop also throws error. You can also see the bottom part of the image for blurry pixels.
Although there is one more case that I know of when image loading fails is when loading an image in CMYK color format. Android doesn't support CMYK, although there are some Third-party libraries for loading CMYK images but I don't think that it is the case for you. I strongly believe that the image is corrupted.
imageLoader = ImageLoader.getInstance();
prova = context;
options = new DisplayImageOptions.Builder()
//.displayer(new RoundedBitmapDisplayer(1000))
.showImageOnLoading(R.drawable.placeholder)
.showImageForEmptyUri(R.drawable.placeholder)
.showImageOnFail(R.drawable.placeholder)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
// .displayer(new RoundedBitmapDisplayer(70))//////
.build();
//note you use this in your adapter where your handling your image and make sure you inistalize that your UIL class in your manifest application
<application
android:name="com.example.UILApplication"
I've code to display image in list view using imageLoader library from com.nostra13.universalimageloader
this code like this
imageLoader.displayImage(link_url, imageView, options, animateFirstListener);
but this code make this app must download image from this url. I wanna ask , can I using this cache image to show image. So if not connect internet the image can be show because this image was save as cache before?? How to show image in imageview if this image is from cache?? thanks
Instead of downloading it again you should cache images in phone memory or sdcard.
It caches images in say sdcard (if you have configured properly) using url as the key. If present display from cache else download, cache and display images.
In your custom adapter constructor
File cacheDir = StorageUtils.getOwnCacheDirectory(activity context, "cache_folder")
imageLoader = ImageLoader.getInstance();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
.discCache(new UnlimitedDiscCache(cacheDir))
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.enableLogging()
.build();
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_id)//display stub image untik image is loaded
.cacheInMemory()
.cacheOnDisc()
.displayer(new RoundedBitmapDisplayer(20))
.build();
In your getView()
viewholder.image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(imageurl, viewholder.image,options);
By deafult it stores cache i guess.
Check once Disconnecting internet.
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.deafaultimage).cacheInMemory()
.cacheOnDisc().bitmapConfig(Bitmap.Config.RGB_565).build();
HTTP caching depends on Cache-Control headers. If the server is sending the proper headers in the response then the library will automatically cache the resource automatically and access it even if there is no network. You don't have to do anything explicitly.
You can configure more options such as cache size etc if you want. See the docs.
I'm using the Universal Image Loader 1.8.6 library for loading dinamically images taken from web.
The ImageLoaderConfiguration configuration is the following:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.threadPoolSize(3) // default
.threadPriority(Thread.NORM_PRIORITY - 1) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCacheSize(2 * 1024 * 1024)
.memoryCacheSizePercentage(13) // default
.discCacheSize(50 * 1024 * 1024)
.discCacheFileCount(100)
.imageDownloader(new BaseImageDownloader(getApplicationContext())) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs()
.build();
and the DisplayImageOptions configuration is:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.no_foto)
.showImageForEmptyUri(R.drawable.no_foto)
.showImageOnFail(R.drawable.no_foto)
.build();
The method getView inside my ArrayAdapter contains the code:
iv.setImageResource(R.drawable.no_foto);
imageLoader.displayImage(basePath+immagine, iv);
The first line of the above code is used just for avoid that the recycling of view in gridView allow to set image in the wrong position (until the picture has not been downloaded).
The actual problem is this:
If I open my Activity (containing the GridView) the shown items thanks to the UIL library can download the image. (Meanwhile the no_foto.png image is shown in each view of the grid). When all the views have loaded their own images, if I try to scroll down and then I come back (scrolling up) I have to wait that the images are reloaded, because all the views are now occupied by the no_foto.png image.
How can I avoid the reload of these images? Using Universal Image Loader, can I cache the images previous loaded?
NOTE: Since my grid can contain many images I would to use an implementation of disk cache (not memory cache).
I found that .discCacheFileCount() can be used for setting the maximum number of cached files, so why it seems that my files are not cached?
I have solved the problem
I was just declaring option, but I wans't using it, so I have modified the line:
imageLoader.displayImage(basePath+immagine, iv);
into:
imageLoader.displayImage(basePath+immagine, iv, options);
and I have added in the options the method:
.cacheOnDisc(true)
Caching is not enabled by default in UIL, so if you want use the cache you should use
// Create default options which will be used for every
// displayImage(...) call if no options will be passed to this method
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
...
.cacheInMemory()
.cacheOnDisc()
...
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
...
.defaultDisplayImageOptions(defaultOptions)
...
.build();
ImageLoader.getInstance().init(config); // Do it on Application start
And while loading image use:
// Then later, when you want to display image
ImageLoader.getInstance().displayImage(imageUrl, imageView); // Default options will be used
Another way is
DisplayImageOptions options = new DisplayImageOptions.Builder()
...
.cacheInMemory()
.cacheOnDisc()
...
.build();
ImageLoader.getInstance().displayImage(imageUrl, imageView, options);
And you can find more information here
I am making an android application on a library which will look like this
I need to put all those book dynamically and all should be individually clickable after clicking a single book it will be open through default pdf reader.
My problem is using list view is it possible if not then what will be suitable approach.And please show me some example I can't find any.
http://code.google.com/p/shelves/. A project by Romain GUy. Worth having a look at the project.
http://code.google.com/p/shelves/source/browse/trunk/Shelves/src.
http://shelves.googlecode.com/svn/trunk/Shelves/. Its a read only. open the trunk and copy the code and use the same.
Use a gridview.
http://developer.android.com/guide/topics/ui/layout/gridview.html
Universal Image Loadeer.
https://github.com/nostra13/Android-Universal-Image-Loader.
It is based on Lazy List(works on same principle). But it has lot of other configurations. I would prefer to use Universal Image Loader coz it gives you more configuration options. You can display a error image if downlaod failed. Can display images with rounded corners. Can cache on disc or memory. Can compress image.
In your custom adapter constructor
File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");
// 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)//display stub image
.cacheInMemory()
.cacheOnDisc()
.displayer(new RoundedBitmapDisplayer(20))
.build();
In your getView()
ImageView image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options.
You can configure with other options to suit your needs.
Along with lazy loading/Universal Image Loader you can view holder for smooth scrolling and performance. http://developer.android.com/training/improving-layouts/smooth-scrolling.html.
Edit:
https://github.com/androidnerds/shelves. Link to download as zip.
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,
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.
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.