2
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;
}

Can I split the byte array into multiple sections and then run encoding on them on different threads so that it could be fast for larger images and then join them at last.

Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
INDER
  • 343
  • 1
  • 4
  • 15
  • can you explain exactly what you want to achieve? I do not think it will do any good with what you are asking – Calvin Jan 19 '16 at 05:43

3 Answers3

1

If you process your bytes by a multiple of 3 at a time it actually is possible as shown in this post.

Otherwise that will not work because of the way the Base64 algorithm works. When you split the array you might have more or less padding per segment so that when you joined the String together in the end and tried to do the reverse operation (back to a byte[]) it would likely fail.

Here is a small example:

String test = "This is a test";
byte[] testBytes = test.getBytes();
int mid = testBytes.length / 2;

byte[] part1 = Arrays.copyOfRange(testBytes, 0, mid);
byte[] part2 = Arrays.copyOfRange(testBytes, mid, testBytes.length);

Encoder base64Encoder = Base64.getEncoder();
System.out.println(base64Encoder.encodeToString(testBytes));
System.out.println(base64Encoder.encodeToString(part1));
System.out.println(base64Encoder.encodeToString(part2));

This will output:

VGhpcyBpcyBhIHRlc3Q=
VGhpcyBpcw==
IGEgdGVzdA==

Notice if you put the second two Strings together they do not equal the first. The = characters are the padding.

If you are worried about the efficiency of Base64 encoding an image you can do the operation on a single background thread. You can use an AsyncTask as one option to help you to do so.

Community
  • 1
  • 1
George Mulligan
  • 11,813
  • 6
  • 37
  • 50
1

For encoding base64 partially, first you need to understand how base64 encoding work. Check below URL for it.

  1. http://www.hcidata.info/base64.htm

  2. https://blogs.oracle.com/rammenon/entry/base64_explained

For eg in case of string. You just need to divide the array of string into the arrays of size 3 for example if you want to convert "12345678aa" in base64 then

convert in below sequence

  1. 123 - MTIz
  2. 456 - NDU2
  3. 78a - Nzhh
  4. a - YQ==

Now just merge you result. Which will become "MTIzNDU2NzhhYQ==". Which is the encoded value of "12345678aa".

Chears................. :)

Ashish Rajvanshi
  • 466
  • 4
  • 15
1

You forgot to decode bitmap before converting it to byte array.

add the following line

 Bitmap bm = BitmapFactory.decodeFile(image);

after

System.gc();  //For memory efficiency

So your code will be as follows:

    public String encodeTobase64(Bitmap image){

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