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.