1

I was creating a sample app for uploading images to server and i found on the stackoverflow about compressing and encoding the image to base64 before uploading.

My Code:

public String encodeTobase64(Bitmap image)
    {

       System.gc();  //For memory efficiency
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String imageEncoded = null;
         imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
        return imageEncoded;
    }

The main problem that has arisen is that when i try to encode a small size image around 2-3 kb it encodes it in a flash and provides me the encoded string in a textview (just for checking if encoding or not)...but when i try to encode a image of large size like more than 1 mb , the app hangs and after 5 minutes it provides the answer and until this duration app is unaccesible.

Even though i have created a new thread for this encoded method the encoding is not fast enough ...

So i wanted to know if there was any other way of encoding more efficiently and fast or if i could make this method more efficient and fast

Shriram
  • 4,343
  • 8
  • 37
  • 64
INDER
  • 343
  • 1
  • 4
  • 15
  • Why cant you directly upload the image without `base64`? – K Neeraj Lal Jan 15 '16 at 14:11
  • *Even though i have created a new thread for this encoded method the encoding is not fast enough* so you think that running the code on another thread would speed it up .... hehehe ... It will always take more time (time needed on same thread + new thread creation) ... only spliting the job would help – Selvin Jan 15 '16 at 14:11
  • 1
    @KNeerajLal ...I have checked online many samples and all show base64 encoding as a way to convert image to string and send over to the server..If you know of any other method pls share i would be happy to know – INDER Jan 15 '16 at 14:14
  • base64 will only make your file larger. you should upload the image directly – Leo Jan 15 '16 at 14:15
  • @Selvin...thats the main problem bcoz the encoding method is inbuilt and to split it up i would have to change it internally – INDER Jan 15 '16 at 14:15
  • 1
    @Leo..How can i upload the image directly ?can you provide any example – INDER Jan 15 '16 at 14:16
  • 1
    http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/ – K Neeraj Lal Jan 15 '16 at 14:18
  • 1
    http://stackoverflow.com/questions/32311484/fastest-way-to-upload-multiple-image-to-server-in-android – K Neeraj Lal Jan 15 '16 at 14:19
  • thank u very much @KNeerajLal – INDER Jan 15 '16 at 14:22

0 Answers0