Search code examples
androidimageurlsaveinternal-storage

Saving and getting image url in internal storage


I am working on an Android application in which I am getting image url from the server. Now I have to do save that image url and image file in my local storage, because for the first time if I want to check if the same url is available in my internal storage then it will get that image file from my local storage, if not then it will first save that image url reference and image file in my internal storage.

My code is given below. I wish to save image url and image file in my internal storage and according to the above conditions.

new DownloadImageTask((ImageView) view.findViewById(R.id.imageViewProfile))
.execute(imageURL);

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }    
    }

Solution

  • If you are looking for saving image once and reusing it many times this may help you Google Volley Library. Universal Image Loader which is widely used after Volley.

    EDIT : Now there are bunch of libaries which are so popular.

    1. Picasso
    2. Glide
    3. Fresco

    Choose according to your requirement.Here is comparison.