0

In Lazy-List i use this code and my image store in a folder named LazyList

ImageLoader imageLoader=new ImageLoader(context); imageLoader.DisplayImage(url, imageView);

But in Universal-Image-Loader i use this

 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())    
            .build();
    ImageLoader.getInstance().init(config);
    img = (ImageView) this.findViewById(R.id.test_id);
    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.displayImage(URL.RECIPE_IMG_URL,img);

but each time it use internet to get image and didn't store my image in any folder i aslo add .diskCache(new UnlimitedDiscCache(new File("myFolder"))) to config but nothing store in myFolder.Any ideas?

Rajesh Mikkilineni
  • 854
  • 10
  • 22
Saeed Masoumi
  • 8,746
  • 7
  • 57
  • 76
  • `new File("myFolder")` it's not a location that your app have access to. You probably want to use `context.getCacheDir()`. – Budius Aug 18 '14 at 16:24
  • is use `context.getCacheDir()` this first then change it to other things but nothing change – Saeed Masoumi Aug 18 '14 at 16:31

2 Answers2

5

You have to save the Image on onLoadingComplete

try this , on onLoadingComplete you have to save the bitmap to a variable bitmap

imageLoader.getInstance().displayImage(imagePath, imageView, options, new SimpleImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {
            spinner.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            String message = null;
            switch (failReason.getType()) {
            case IO_ERROR:
                message = "Input/Output error";
                break;
            case DECODING_ERROR:
                message = "Image can't be decoded";
                break;
            case NETWORK_DENIED:
                message = "Downloads are denied";
                break;
            case OUT_OF_MEMORY:
                message = "Out Of Memory error";
                break;
            case UNKNOWN:
                message = "Unknown error";
                break;
            }


        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

            showedImgae = loadedImage;

        }
    });

now onclick on save Button / if you want to save that Bitmap/Image to SDCard use this

 public void downloadImage(){

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/DCIM/youfoldername");    
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "imagename-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
        FileOutputStream out = new FileOutputStream(file);
        showedImgae.compress(Bitmap.CompressFormat.JPEG, 100, out);
        Toast.makeText(MyActivity.this, "Image Saved", Toast.LENGTH_SHORT).show();
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    getApplicationContext().sendBroadcast(mediaScanIntent);
}

Hope it helps you..

Rajesh Mikkilineni
  • 854
  • 10
  • 22
4

I found my answer . By default caching is disable i should add DisplayImageOptions to ImageLoaderConfiguration

And here is code

   DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .build();
Saeed Masoumi
  • 8,746
  • 7
  • 57
  • 76
  • Are you able to save images to your phone using UIL after this code change ? – codeX Aug 18 '15 at 17:18
  • Thats great . I added this code in ImageLoaderConfiguration but am not getting it saved . Could you please brief me the steps ?Have you made any other changes ? – codeX Aug 19 '15 at 07:17