2

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

Community
  • 1
  • 1
Milad
  • 22,895
  • 9
  • 62
  • 76
  • You cannot compare as in the first you send a file and in the second you start with an image which will be a Bitmap i think. Start both with a file or both with a Bitmap. – greenapps Mar 27 '15 at 21:26
  • I think an image , is - A - File , isn't it ? – Milad Mar 27 '15 at 21:31
  • No. And not in your code. Please start in both examples with or a file or a Bitmap. – greenapps Mar 27 '15 at 21:33
  • In your first example i see a file (image_file_path). You should use image_file_path too in the second example so we can see that you try to upload the same file/image. – greenapps Mar 27 '15 at 21:40
  • Thank you. I see that your code does not do what you explained /asked in the text. In the first example you send the contents of a file. Now i would expect that in the second example you would send the base64 encoded content of that file. But you are not doing that. Instead you do all kind of manupulations on its content and finally send the base64 encoded result of those manupulations. But what are you comparing than? – greenapps Mar 27 '15 at 21:51
  • @greenapps , FYI , to use base64encode in android , READ this http://stackoverflow.com/questions/4830711/how-to-convert-a-image-into-base64-string – Milad Mar 27 '15 at 22:21
  • Please neglect the checked answer there as that is the same as your second example and principally wrong. The better one is the answer of Chandra S and the comment of ElYeante on it. – greenapps Mar 27 '15 at 22:58

1 Answers1

1

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.

greenapps
  • 10,799
  • 2
  • 13
  • 19