dhamini poornachandra
dhamini poornachandra

Reputation: 472

Dynamically load a list of images based on server response

I am sending a request to a server and the server sends a list of appliance names. I want to load images based on the appliance names returned by the server, right now the server has 200 types of appliance names, and the server returns a list of random appliance name each time I send a request. Is there an efficient way to load images based on the response server. Any help is appreciated. the response is as follows:

"appliance":

{
    "id": 1,
    "type": "electronic",
    "make": "2015",
    "model": "2011",
    "serialNumber": "19218hdyew",
    "serviceTagNumber": "hfh251663",
    "name": "fridge"
},
{
    "id": 2,
    "type": "electronic",
    "make": "2015",
    "model": "2011",
    "serialNumber": "19218hdyew",
    "serviceTagNumber": "hfh251663",
    "name": "coffee"
},
{
    "id": 3,
    "type": "electronic",
    "make": "2015",
    "model": "2011",
    "serialNumber": "19218hdyew",
    "serviceTagNumber": "hfh251663",
    "name": "washing machine"
},
{
    "id": 4,
    "type": "electronic",
    "make": "2015",
    "model": "2011",
    "serialNumber": "19218hdyew",
    "serviceTagNumber": "hfh251663",
    "name": "fan"
},
{
    "id": 5,
    "type": "electronic",
    "make": "2015",
    "model": "2011",
    "serialNumber": "19218hdyew",
    "serviceTagNumber": "hfh251663",
    "name": "tv"
}

Upvotes: 2

Views: 276

Answers (4)

karanatwal.github.io
karanatwal.github.io

Reputation: 3673

Your server is returning image names, then you should create a method like

displayimage(String imagename)

where imagename is string you are getting from server.

In displayimage method you can use setimageresource

Upvotes: 1

Newbie
Newbie

Reputation: 368

You need to show the images from drawable according to the server response. If you have the option to keep the images in assets folder,

then here is the solution,

  1. Move your images to assets folder.
  2. Then try the below code.

    AssetManager assetManager = context.getAssets();
    InputStream inputStream = null;
    
    try {
        inputStream = assetManager.open(imageName + ".extension");
    }    
    catch (IOException e) {
        e.printStackTrace();
    } 
    
    if (inputStream != null) {
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        imageView.setImageBitmap(bitmap);
    }
    

Upvotes: 0

Arjun saini
Arjun saini

Reputation: 4182

Used the Android Universal Image-Loader This is best

Declare

    private ImageLoader imageLoader1;

On Create

 imageLoader1 = ImageLoader.getInstance();

 imageLoader1.init(ImageLoaderConfiguration.createDefault(getActivity()));

no_image here a drawable image without any image load in Cache

   DisplayImageOptions
            options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .showImageOnLoading(R.drawable.no_image) // resource or drawable
            .showImageForEmptyUri(R.drawable.no_image) // resource or drawable
            .showImageOnFail(R.drawable.no_image)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    imageLoader1.displayImage(yourpath.replace(" ", "%20"), ivprofile, options);

Use Also It's dependency

with jar

  compile files('libs/universal-image-loader-1.9.5.jar')

and

  compile  'com.nostra13.universalimageloader:universal-image-loader:1.9.1'

You Can Use Piccaso Also and Glide also

Upvotes: 0

Ravi
Ravi

Reputation: 35569

First of all fetch response from server and parse it. Refer volley or Retrofit for more details.

After getting response from server, fetch imageUrl from it and perform lazy loading. Refer Picasso, Fresco, UniversalImageLoader, Glide.

To get imageUrl you need to concat imageName with your server url where actual image is located or tell your API developer to give full URL of image in webservice.

you can check this thread for Lazy loading

Upvotes: 0

Related Questions