My app receive a jsonArray that contain names of categories and their images urls. which is the best to do:
send the url of the images and after that download them.
or instead of sending the url I will send the images converted with base64.
actually I know that base64 is more larger, but I just want to do a trade off between the size and the number of the requests, I mean which is better: to get one large request, or get a lot of small request ?
I have about 20 categories and the image is just 20-30 Kb.
1: Send the image urls and download and heavily cache device side, this tends to 'Always' be the best way.
The JSON response should have an array of your objects each with a name and a URL, simply get the JSON response in your app and parse out the relevant URLS and names, downloading each using Picasso (As discussed in earlier question) as you go, it will cache them for faster access in future also.
Related
I'm building a RestAPI for a mobile application where you can upload images and stuff.
My question was about how you pass actual images to the RestAPI.
My design is:
You have an endpoint /api/data/createReport
This endpoint accepts a field called images which is a comma-separated list of strings, which are the base64 encoded images.
I would then split the strings and store them somewhere (or maybe re-encode them as images).
Is this a good design?Is it safe?Is it performant?
Thanks in advance!
In my android application when a user login from a new device i want to download all his data from the server database that may be more than 20mb in size.This data include bitmap images that are converted to string.When a user uploads a image what i done so far is i just convert this image to bitmap and then convert it to string and then save this to database,after that this data will save to server database by syncing.If the same user login from a new device i need to take all those data from server with a webservice.Right now i am using resttemplate to load this data, but the problem is when there is more than 20mb of data that mainly contains some image data the webservice may take more time based on the image size.Is there any better way to deal with images??
You can use aws s3. The images can be saved in the s3 buckets, which provides us with the URL. While loading all the details in the app instead of loading the whole image, only the s3 URLs needed to be send. The images can be downloaded later while loading them into the imageviews using libraries like Picasso or glide.
Do not use bitmap images (what ever you mean by it) but jpg's. Dont convert them to base64 as the amount of data will increase by 30%. Dont save them in a database on your server but on the server file system as normal files.
Then just give twenly urls for twenty files to the Android client and let the Android client decide when and how to download the images from url.
Alternatively you can let your base64 encoded images in the database. Just send twenty ids or file names (for twenty files) to the Android client. Then let the Android client decide when it wants to download an image using an url with parameter id.
I have a json from server in which the image urls are send from server but i want to know if i ask my php developer person to send encoded image in json would be a better approch or the former one ?
I want to load the images in listview after that.
Thanks in Advance.
getting url will be better and clean approach.it will make your response compact and image url can be loaded any time you want where if you transmit encoded image it has to be transmitted in network even if you dont want to display at the time.
I need a workaround for the following task:
I have a JPG (of whatever) picture on my sd card, and I need to send it to another device in the background. How should I do that?
Best way (in theory) would be via MMS, but after a lof of searching, I can say that there is no official and trustful (and working) way to do that in the background.
Any ideas, samples, even proofs that it can be done are welcomed! All that matters is that a remote device must have access to that image.
if you want the sending to happen in background you could use android beam, but you would have to get both devices cloth together.
And as i am not sure about what u mean by background i can't be sure that thats what you want. :)
First of all you need to create a "Service" in App which will run in background and do all tasks given below. A central PHP Server required for this task. Other device can download that file by the same HTTP request method.
Convert image to base64 string--
How to convert a image into Base64 string?
you can convert byte array to suitable types- string or delimiter(, or .) separated string
Then create a HTTP request--
Make an HTTP request with android
for HTTP request create a url like this - https://www.yoursite.com/post/?code="base64 string goes here"
-Receive data in php file on your server by $_GET global array
$code = $_GET['code']
In php file convert base64 code to original image.
How to decode a base64 string (gif) into image in PHP / HTML
get image from base64 string
Maybe I explained badly my needs. An important thing I missed is that the same person has access to both device. I solved it by uploading the image to google drive.
I have the following code:
String response = webService.webGet(""); the response of the web service
String LargeImage = new Gson().fromJson(response,String.class);
byte[] imageByteArray = Base64.decode(LargeImage);
response is like: "iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCA... " a Base64 encoded image having around 400.000 characters.
The response comes very quick from the web service. When it tries to parse it with Gson after a while (like 20-30 seconds) I get an error with out of memory. How can I parse this simple string without Gson, it should be fairly simple but I don't know how to do it.
Please advise me. Thank you for your time
Change your Base64 image encoding into a URL where you can download the image directly.
Then go get flexjson 2.1 and you can parse JSON on Android very easily in a few lines. It's also faster than GSON.
http://flexjson.sourceforge.net
But from your post it looks like you're just sending the Base64 image over JSON as a single string. No need to use JSON in that case. Unless you plan on wrapping some metadata around it in the future.
Also skip storing the image in the DB. Just write it to the filesystem, and put the file path in the DB linked to your object. Much easier to debug when you wonder what image you downloaded, etc.
response is like: "iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCA... " a Base64 encoded image having around 400.000 characters.
You have got to be kidding me.
Please advise me.
Find a sensible Web service and switch to it. If somebody at your firm wrote the Web service, fire them. If you wrote the Web service, fire yourself. I see no need to be returning an image -- particularly one that massive -- in Base64 encoding, wrapped in JSON.
In the meantime, you could try the built-in Android JSON parser rather than Gson.