Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
Posts: 97
Registered: ‎07-12-2011
My Device: curve 8900
My Carrier: Airtel

how to create cache directory for the Application..

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

Developer
Posts: 208
Registered: ‎08-04-2009
My Device: Not Specified

Re: how to create cache directory for the Application..

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).

Regular Contributor
Posts: 97
Registered: ‎07-12-2011
My Device: curve 8900
My Carrier: Airtel

Re: how to create cache directory for the Application..

Thanks for reply.. To download image im using thread.. How to file system to save the cache..
Developer
Posts: 208
Registered: ‎08-04-2009
My Device: Not Specified

Re: how to create cache directory for the Application..

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. 

Regular Contributor
Posts: 97
Registered: ‎07-12-2011
My Device: curve 8900
My Carrier: Airtel

Re: how to create cache directory for the Application..

hi.. i created hashtable.. i put images like that

public void put(String id, Bitmap bitmap){
cache.put(id, bitmap);

}

how to get the bitmap from hashtable..
public Bitmap get(String id){
if(!cache.containsKey(id))
return null;
Bitmap b = (Bitmap) cache.get(id);

return b;
}

but it returns null.. pls help out to get the bitmap
Developer
Posts: 208
Registered: ‎08-04-2009
My Device: Not Specified

Re: how to create cache directory for the Application..

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.

Regular Contributor
Posts: 97
Registered: ‎07-12-2011
My Device: curve 8900
My Carrier: Airtel

Re: how to create cache directory for the Application..

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(url)));
        }
        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.toString());
            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();
    }
}


Regular Contributor
Posts: 97
Registered: ‎07-12-2011
My Device: curve 8900
My Carrier: Airtel

Re: how to create cache directory for the Application..

hi.. pls anyone help. how to save cache in File System..