Reputation: 8435
I am downloading images from server into the ListView
, now to perform this task i am using ImageDownloader example code. so far its working fine.
But i want to save
images of ListView
in a SD card
but i am confused when to store the images as images are being downloaded Asynchronously and because of ViewHolder pattern its little tough for me to judge.
Once i stored it in a SD card next time i want to read it from memory only.
ImageDownload is storing bitmap in cache and fetching it from there once it gets downloaded.But the problem is its behavior is not predictable.
Sometimes it downloads from server and sometimes from cache.
so can anyone help me in finding what is the proper place to store the images in sd card once.
Upvotes: 0
Views: 432
Reputation: 1530
Modify your ImageDownloader class to save the image like this :
download(String url, ImageView imageView, Boolean saveData)
private Boolean saveData;
and store in it the value given as parameter in download dmethod:
this.saveData = saveData;
@Override protected void onPostExecute(Bitmap bitmap) { if (isCancelled()) { bitmap = null; }
addBitmapToCache(url, bitmap); if (saveData == true) { try { FileOutputStream out = new FileOutputStream(path); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); } catch (Exception e) { e.printStackTrace(); } } if (imageViewReference != null) { ImageView imageView = imageViewReference.get(); BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView); // Change bitmap only if this process is still associated with it if (this == bitmapDownloaderTask) { imageView.setImageBitmap(bitmap); } } }
where path is the path were you want to save the image .
and next time before you want to load the image you have to see if it is already downloaded and load it from the path otherwise call ImageDownloader.
that's it! enjoy!
Upvotes: 1