How to save images downloaded via UrlImageViewHelper to SD card on Android?

386 Views Asked by At

I'm using UrlImageViewHelper library and it works fine. It caches the images on internal storage and it's bad for me because I've about lots of images and if I want to cache them it's horrible. How can I save these downloaded files and store them in SD card instead of internal storage?

2

There are 2 best solutions below

0
chet's On BEST ANSWER

Please check your whether your URL is valid or not by putting into browser. If image size is large then please use placeholder image which still displays your URL, not load image available, like this:

UrlImageViewHelper.setUrlDrawable(imageView, "http://example.com/image.png", R.drawable.placeholder);
0
Pawan asati On

Use this class

public class DemoHelper {

    private static final String TAG = DemoMainActivity.TAG;

    public static class RemoteImageTask extends AsyncTask<Void, Void, Bitmap> {
        ImageView _image;
        String _imageSource;
        TaskCallback _callback;

        public RemoteImageTask(ImageView image, String imageSource) {
            this(image, imageSource, null);
        }

        public RemoteImageTask(ImageView image, String imageSource, TaskCallback callback) {
            _image = image;
            _imageSource = imageSource;
            _callback = callback;
        }

        protected Bitmap doInBackground(Void... params) {
            URL url;
            Bitmap bmp = null;
            try {
                url = new URL(_imageSource);
                bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (Exception ignored) {
                Log.e(TAG, "Exception", ignored);
            }

            return bmp;
        }

        protected void onPostExecute(Bitmap bmp) {
            _image.setImageBitmap(bmp);
            if (_callback != null)
                _callback.onTaskFinished(bmp);
        }
    }

    public interface TaskCallback {
        public void onTaskFinished(final Bitmap bmp);
    }
   }