Milad
Milad

Reputation: 28592

android :uploading image to server , base64encoded or multipart/form-data?

In my android application , user can upload a 300kb image;

I'm going to use This ( Android Asynchronous Http Client ) which I think is great and also Whatsapp is one of it's users.

In this library , I can use a RequestParams ( which is provided by apache I think) , and add either a file to it or an string ( lots of others too).

here it is :

1- Adding a file which is my image ( I think as a multipart/form-data)

   RequestParams params = new RequestParams();
   String contentType = RequestParams.APPLICATION_OCTET_STREAM;
   params.put("my_image", new File(image_file_path), contentType); // here I added my Imagefile direcyly without base64ing it.
   .
   .
   .
   client.post(url, params, responseHandler);

2- Sending as string ( So it would be base64encoded)

   File fileName = new File(image_file_path);
   InputStream inputStream = new FileInputStream(fileName);
   byte[] bytes;
   byte[] buffer = new byte[8192];
   int bytesRead;
   ByteArrayOutputStream output = new ByteArrayOutputStream();
   try {
       while ((bytesRead = inputStream.read(buffer)) != -1) {
       output.write(buffer, 0, bytesRead);
   }
   } catch (IOException e) {
   e.printStackTrace();
   }
   bytes = output.toByteArray();
   String encoded_image = Base64.encodeToString(bytes, Base64.DEFAULT);

   // then add it to params : 

   params.add("my_image",encoded_image);

  // And the rest is the same as above

So my Question is :

Which one is better in sake of Speed and Higher Quality ?

What are the differences ?

NOTE :

I've read many answers to similar questions , but none of them actually answers this question , For example This One

Upvotes: 2

Views: 2067

Answers (1)

greenapps
greenapps

Reputation: 11214

Don't know if params.put() and params.add would cause for a change of multipart encoding.

The base64 endoded data would transfer 30% slower as there are 30% more bytes to transfer.

What you mean by quality i do not know. The quality of the uploaded images would be equal as they would be byte by byte the same to the original.

Upvotes: 1

Related Questions