Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

Should one use static variables or store data in database

Hi,
Although my question arose from building an android application but i think its a general programming question in java.

I hava a set of three images which i want to load. Now loading the images takes time so there is a lag of around 2 seconds. What i am thinking is of maintaining a static variable array which will store the Bitmap of all the images.
This way when the user comes back to the screen i dont have to reload the Bitmap images but just take them from the Bitmap static array....

Although i am wondering if it can be stored in some other way say a db?

I want to know the pros and cons of storing a static variable and storing data in a database.
Is it a good idea to store the Bitmap somwhere else or store them in a static array ?
Can storing in static variable lead to bugs ? or is it a good programming practice ?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rohit Bajaj

ASKER

thanks for explanation.
so say i create a static array of 3 elements each containing a bitmap.  now i initialize it at a point with 3 bitmap.
if later on i need to remove them from ram will just setting the array = null will do or i need to specifically assign null to each element of the array.
Avatar of dpearson
dpearson

Setting the array to null will clear it.

More specifically, the garbage collector will remove the bitmaps when there is no way for you in code to access them any more.  In this case once the array is null, then you can't access the elements in the array so the whole thing gets garbage collected.

Doug
I think loading them as resources is essentially equivalent to having them on the local filesystem - it's another way to store the image.

I was assuming that the images were being initially loaded over the network and hence why there was a noticeable delay in getting them loaded?

Doug
The wallpapers are loaded from local disk only not over the network.
However the retreiving the Bitmap from the wallpaperName is taking around 2 seconds.
I am using the following function to retrieve the Bitmap image :

 public static Bitmap getBitmapFromWallpaperAsset(Context context, String wallpapername) {
        InputStream inputStream = null;
        try {
            inputStream = context.getAssets().open("wallpapers/" + wallpapername);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return BitmapFactory.decodeStream(inputStream);
    }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial