01-10-2012 07:35 AM
hi.. how to create cache directory for the Application.. how to create Hashtable and put image and image url.. how to retrieve that.. my situation is i load the images from server.. when i login i cant load images because its takes lot of time.. i have save the images in hashtable and retrieve it in next login time.. as well as i have load images fastly like android.. how to do that.. pls give some sample code.. to create hashtable and strore images and retrieve it.. help me out friends
01-10-2012 07:40 AM
To load images quickly you would need to use threads to download bitmaps from the sever. To save images in cache, you can use persistent storage, db, or file-system, I would prefer File System, because small memory size(application heap).
01-10-2012 08:27 AM
01-11-2012 12:09 AM
You can use any directory in sdcard, to use caching on file-system, and maintain a hashtable of url and file_path to maintain the cache, on application exit you can delete these files. Please look for writing/reading files for this.
01-11-2012 01:26 AM
01-11-2012 04:06 AM
Please check if you are using same cache object on which you are putting your bitmap object, and my other suggestion to not use hashtable to store bitmap, because by this way you may lead to outOfMemoryError, please put your code here, so we can edit and update it accordingly.
01-11-2012 04:49 AM
my image Loader code
public class ImageLoader {
//the simplest in-memory cache implementation. This should be replaced with something like SoftReference or BitmapOptions.inPurgeable(since 1.6)
private Hashtable cache=new Hashtable();
MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private FileConnection cacheDir;
public static EncodedImage image = null;
public static Bitmap bitmap = null;
public ImageLoader() {
//Make the background thead low priority. This way it will not affect the UI performance
try
{
fileCache = new FileCache();
System.out.println("ImageLoader");
}
catch(IOException ex)
{
System.out.println("IOException "+ex);
}
}
public void DisplayImage(String url, BitmapDowloadListener listener)
{
cache.put(url, listener);
System.out.println("cache size "+cache.size());
if(cache.containsKey(url))
{
listener.ImageDownloadCompleted((memoryCache.get(
}
else
{
queuePhoto(url, listener);
}
}
private void queuePhoto(String url, BitmapDowloadListener listener)
{
//This ImageView may be used for other images before. So there may be some old tasks in the queue. We need to discard them.
PhotoToLoad p=new PhotoToLoad(url, listener);
photoLoaderThread = new Thread(new PhotosLoader(p));
photoLoaderThread.start();
}
private Bitmap getBitmap(String url)
{
//I identify images by hashcode. Not a perfect solution, good for the demo.
//Code to return Bitmap
HttpConnection connection = null;
DataInputStream inputStream = null;
try
{
connection = (HttpConnection) Connector.open(url, Connector.READ, true);
System.out.println("connection "+connection);
inputStream = new DataInputStream(connection.openDataInputStream() );
//inputStream = connection.openInputStream();
System.out.println("inputStream"+inputStream);
byte[] responseData = new byte[10000];
int length = 0;
StringBuffer rawResponse = new StringBuffer();
while (-1 != (length = inputStream.read(responseData)))
{
rawResponse.append(new String(responseData, 0, length));
}
// System.out.println("rawResponse"+rawResponse.toStr
int responseCode = connection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK)
{
System.out.println("HTTP response code:"+responseCode);
throw new IOException("HTTP response code: "
+ responseCode);
}
final String result = rawResponse.toString();
byte[] dataArray = result.getBytes();
System.out.println("dataArray" +dataArray);
image = EncodedImage.createEncodedImage(dataArray, 0,
dataArray.length);
System.out.println("bitmap image data" +image);
bitmap = image.getBitmap();
return bitmap;
}
catch (final Exception ex)
{
System.out.println("Exception:"+ex.getMessage());
return null;
}
finally
{
try
{
inputStream.close();
inputStream = null;
connection.close();
connection = null;
}
catch(Exception e){}
}
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
return null;
}
//Task for the queue
private class PhotoToLoad
{
public String url;
public BitmapDowloadListener listener;
public PhotoToLoad(String u, BitmapDowloadListener listener){
this.url=u;
this.listener=listener;
}
}
PhotosQueue photosQueue=new PhotosQueue();
private Thread photoLoaderThread;
public void stopThread()
{
photoLoaderThread.interrupt();
}
//stores list of photos to download
class PhotosQueue
{
private Stack photosToLoad = new Stack();
//removes all instances of this ImageView
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad=photoToLoad;
}
public void run() {
try{
//thread waits until there are any images to load in the queue
System.out.println("photoToLoad.url "+photoToLoad.url);
Bitmap bmp=getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
photoToLoad.listener.ImageDownloadCompleted(bmp);
}
catch(Exception ex)
{
}
}
public void clearCache() {
//clear memory cache
try
{
memoryCache.clear();
fileCache.clear();
cache.clear();
}
catch(Exception ex)
{
System.out.println("clear cache ex" +ex);
}
}
}
*****************************
my memory cache code
public class MemoryCache {
private Hashtable cache=new Hashtable();
public Bitmap get(String id){
if(!cache.containsKey(id))
return null;
Bitmap b = (Bitmap) cache.get(id);
System.out.println("bitmap here"+b);
return b;
}
public void put(String id, Bitmap bitmap){
System.out.println("memory cache put things");
cache.put(id, bitmap);
}
public void clear() {
cache.clear();
}
}
01-11-2012 06:39 AM