-1

I am new to android. I have some images stored at some path on server. In my application i want that the images should be loaded once from the server and next time the user opens the application the app should not load it again. Please tell me how should i store these images. Please help.

USER9561
  • 1,084
  • 3
  • 15
  • 41
  • Whether these images will not change, any time? – Nigam Patro Dec 04 '15 at 05:37
  • Do a thing, save image to gallery when it gets download first time....and check if you have that image in gallery then don't download.. And set path of that gallery into your imageview. – Anshul Tyagi Dec 04 '15 at 05:41

7 Answers7

2

For store image on app cache or internal storage(sdcard0) you can use library like Universal-Image-Loader , Aquery, Picasso etc.

These library helps you to make image loading faster, save images on cache memory or data.

You can download this library from here

Universal image loader has more functionality than Aquery.

Hope this helps you out.

Jenisha Makadiya
  • 832
  • 5
  • 17
0

You can store image in file. You need only set unique name to image file, an example md5 hash of url.

You can save image in file use follow code:

File file = File(filePath)
    try {
        FileOutputStream outStream = FileOutputStream(file)
        bitmap!!.compress(Bitmap.CompressFormat.PNG, 100, outStream)
        outStream.flush()
        outStream.close()
    }catch(e: Exception){
        Log.e("log", "error save image to cache $filePath")
    }
}

and read from file:

    try{
        BitmapFactory.Options optionsBitmapFactory = BitmapFactory.Options()
        optionsBitmapFactory.inPreferredConfig = Bitmap.Config.ARGB_8888
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, optionsBitmapFactory)
    }catch(e: Exception){
        Log.e("log", "error load image from cache $filePath")
    }
Kota1921
  • 2,451
  • 1
  • 10
  • 13
0

There are several image loader libraries for it. One of them is Universal Image loader and Glide

You can refer this Picasso v/s Imageloader v/s Fresco vs Glide

It will store images in cache and next time will provide image from cache. For server images Universal Image loader and Glide library is highly recommended. You can also set placeholder image.

Community
  • 1
  • 1
Beena
  • 2,334
  • 24
  • 50
0

There are two way :-

  1. Do cache images for an example just look at picaso library.

    //Code how to use picaso :-

    /Initialize ImageView
    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    
    //Loading image from below url into imageView
    
    Picasso.with(this)
       .load("YOUR IMAGE URL HERE")
       .into(imageView);
    
  2. Store images to storage either external or internal and fetch second time from storage. you can use again picaso callback to save bitmap cio.economictimes.indiatimes.com

Praveen Sharma
  • 4,326
  • 5
  • 25
  • 45
0

There are some third party libraries available for these purpose which would save some development effort.

Below are some of them:

Jayakrishnan Salim
  • 977
  • 1
  • 10
  • 24
0

Here is some libs for loading image from URL and it will store image in to memory as cache.

UIL : flexible and highly customizable instrument for image loading, caching and displaying. It provides a lot of configuration options and good control over the image loading and caching process.

PICASSO : Handling ImageView recycling and download cancelation in an adapter. Complex image transformations with minimal memory use. Automatic memory and disk caching.

Bhavesh Rangani
  • 1,490
  • 12
  • 23
0

First you must make sure your application has permission to write to the sdcard. To do this you need to add the uses permission write external storage in your applications manifest file. See Setting Android Permissions

Then you can you can download the URL to a file on the sdcard. A simple way is:

URL url = new URL ("file://some/path/anImage.png");
InputStream input = url.openStream();
try {
    //The sdcard directory e.g. '/sdcard' can be used directly, or 
    //more safely abstracted with getExternalStorageDirectory()
    File storagePath = Environment.getExternalStorageDirectory();
    OutputStream output = new FileOutputStream (new File(storagePath,"myImage.png"));
    try {
        byte[] buffer = new byte[aReasonableSize];
        int bytesRead = 0;
        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
            output.write(buffer, 0, bytesRead);
        }
    } finally {
        output.close();
    }
} finally {
    input.close();
}

EDIT : Put permission in manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />