Abdullah Khan
Abdullah Khan

Reputation: 56

How to load many of images from server in android fast?

I'have developed an android wallpaper app which have a lot of wallpapers images around 200 wallpapers(high size images). I'm using rest api and retrofit library for that but my app load it slowly now I want to make it faster to load all the wallpaper. Any best approach to make my app faster ?

Upvotes: 0

Views: 2765

Answers (3)

Chisom Emmanuel
Chisom Emmanuel

Reputation: 71

You can load images using Glide

  1. Add mavenCentral() if it's not added automatically by android studio dependency in build.gradle Project for repositories.
    mavenCentral()

  2. Then add the following dependencies to build.gradle app module.
    implementation 'com.github.bumptech.glide:glide:4.12.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

Upvotes: 0

rgaraisayev
rgaraisayev

Reputation: 396

I experienced similar issue when i needed to load lots of pictures. Used Glide with cache but again it was slow. Glide cache is not stable, sometimes it removes images and re-downloads them. So i downloaded all images to cache manually, then loaded them using Glide. It was way faster.

Here are the hints.

Download images to cacheDir and save their location

public static String downloadFile(Activity activity, String fileURL, String fileName, String type) {
        File folder = new File(activity.getCacheDir() + "/dg/");
        folder.mkdirs();
        String rootDir = folder.getAbsolutePath() + "/" + fileName;
        try {
            File rootFile = new File(rootDir);
            if (rootFile.exists()) {
                System.out.println("VIDEO/IMAGE EXISTS:  " + fileName);
                return rootDir;
            }
            URL url = new URL(fileURL);
            HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
            InputStream is = ucon.getInputStream();
            BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
            FileOutputStream outStream = new FileOutputStream(rootFile);
            byte[] buff = new byte[5 * 1024];

            //Read bytes (and store them) until there is nothing more to read(-1)
            int len;
            while ((len = inStream.read(buff)) != -1) {
                outStream.write(buff, 0, len);
            }

            //clean up
            outStream.flush();
            outStream.close();
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rootDir;
    }

Then load them using (GlideApp) in Splash screen maybe? Or with pagination.

 GlideApp
                .with(activity)
                .load(new File(activity.getCacheDir() + "/dg/" + name))
                .diskCacheStrategy(DiskCacheStrategy.DATA)
                .into(imageView);

Upvotes: 1

Nitesh
Nitesh

Reputation: 3903

1) Use some good Image loading library i.e Picasso or Glide. These libraries provide a good image caching functionality with minimum effort.

2) Try to load smaller size images for thumb images in your List or grid and only load high-resolution images when user want to see the full image by clicking on the thumbnail.

3) Use pagination, Initially displays some images say 20 images, and fetch next 20 when the user scrolls down to the bottom of the list and so on.

Upvotes: 0

Related Questions