0

I have an Android app
I also have a web server that has been programmed with Sails.js.
The Android app communicates with this web server.
For example, the Android app sends a request to the server that the server sends back 20 images for the Android app as a response.

Now, my question is, how should I send these images to the Android app?(In Sails.js)

ColdFire
  • 6,764
  • 6
  • 35
  • 51
mas cm
  • 131
  • 2
  • 10

1 Answers1

1

You have two options.

Option 1:

The first request return array of urls, url for each image, and the Android app should use each url to display the image.

For example it can be json response:

{images: [{url: "http://example.com/image1.jpg"}, {url: "http://example.com/image2.jpg"}]}

If the images are static files, you can see here How to serve a static folder in sails.js?

Option 2:

The server can return array of the images themself, each item in the array can be the base64 encoded image data.

So the steps are:

  1. Get the base64 encoded images, if you want to take them from file you can see this question: http://stackoverflow.com/questions/24523532/ddg#24526156
  2. Put the images into array and send it as the http response

    {images:[{data: "data:image/jpeg;base64,/9j/4AAQSkZJRg...."},{data: "data:image/png;base64,/9j/1414...."}]}
    
  3. Display the base64 encoded file in the Android app. I dont have any experience in Android development, but maybe this answer will help you: How to convert a Base64 string into a BitMap image to show it in a ImageView?
A. Krol
  • 26
  • 2
  • I'm currently using Option 1 and I will send the name of all the images as an Json object. Please, if it is possible for you, to more explain Option 2 with one example. – mas cm Apr 10 '18 at 13:25
  • I edited it. Do you have a good reason why to use option 2? I dont see any reason in most cases... – A. Krol Apr 10 '18 at 15:01
  • Thanks a lot. It was very useful – mas cm Apr 10 '18 at 17:23