Search code examples
androidgithubuniversal-image-loader

How to add an ImageLoader in android


I have an android game application in which each level has a 250*250(pixel) image and all the levels are in one Activity named "game1.java". because many layout features change in the levels; I call the same class using Intent and finish() the class when I wanna change the level of the game. in that I have problem in the ram usage and I tried to use this in the game1 class:

https://github.com/nostra13/Android-Universal-Image-Loader

but I get Unfortunately each time I wanna open the activity.

this is an attribute of the game1 class:

ImageLoader imageLoader;

this part is at the start of onCreate:

      super.onCreate(savedInstanceState);

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
    ImageLoader.getInstance().init(config);

    File cacheDir = StorageUtils.getCacheDirectory(getBaseContext());
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .resetViewBeforeLoading(false)  // default
            .delayBeforeLoading(0)
            .cacheInMemory(false) // default
            .cacheOnDisk(true) // default
            .build();

whenever I need a new image i call this function and give the strName name and extension of my image that is in assets folder. for example "image1.jpg"

private Bitmap getBitmapFromAsset(String strName)
{
    String imageUri = "assets://" + strName;

    //Bitmap bmp = ImageLoader.getInstance().loadImageSync(imageUri);
    imageLoader.loadImage(imageUri,new SimpleImageLoadingListener(){
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

            super.onLoadingComplete(imageUri, view, loadedImage);
            bmp_loader=loadedImage;
        }
    });

    return bmp_loader;
}

then I give the image to the ImageView via setImageBitmap function

where is the problem?


Solution

  • using my friends' helps; this is what i did eventually to fix the problem:

    step 1: I setup my loader in the first activity of my game; in the onCreate:

    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    
        ImageLoaderConfiguration config = new ImageLoaderConfiguration
                .Builder(getBaseContext())
                .defaultDisplayImageOptions(defaultOptions)
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .discCacheSize(50 * 1024 * 1024)
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .build();
    

    step 2:

    in the game1.class ; after super.oncreate:

            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(config);
    
        File cacheDir = StorageUtils.getCacheDirectory(getBaseContext());
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .resetViewBeforeLoading(false)  // default
                .delayBeforeLoading(0)
                .cacheInMemory(false) // default
                .cacheOnDisk(true) // default
                .build();
    

    I gave the imageaddress that needed to a string

       ax_level = "level_01.jpg";
    

    defined my imageview

    ImageView posht_safhe = (ImageView) findViewById(R.id.rook_ax);
    

    gave the image to my imageView

    String pin_url = "assets://"+ax_level;
        DisplayImageOptions option = new DisplayImageOptions.Builder()
                .cacheOnDisc(true)
                .cacheInMemory(false )
    
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    
        ImageLoader.getInstance().displayImage(pin_url ,posht_safhe, option, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view,
                                          Bitmap loadedImage) {
    
      //                bitmap = loadedImage;
            }
        });
    

    and finally it worked