Tasked with building a chat client with the telegram API (Post coming soon), I encountered this issue wherein the byte array had to be converted to a base64 string to display a thumbnail.
After a couple of hours of research with no solution in sight of which I did not need aspirin for, the following solution below settled it.
byte[] bytes = new byte[byteArray.size()];
for(inti = 0; i<byteArray.size(); i++){
/*
* In this context the values of the array is parsed as a string hence the method 'getString'.
*/
bytes[i] = Byte.parseByte(byteArray.getString(i));
}
Example with a Base64Converter
public class FileToBase64 {
public static String convertToBase64 (byte[] imageBytes){
return Base64.encodeBase64String(imageBytes);
}
}
Full code
@ReactMethod
public void convertToBase64(Readable ArraybyteArray,Callback callback){
byte[] bytes =new byte[byteArray.size()];
for(inti = 0; i<byteArray.size(); i++){
bytes[i] = Byte.parseByte(byteArray.getString(i));
}
// Regular callback gotten from react native
// see - https://reactnative.dev/docs/native-modules-android#callbacks
callback.invoke( FileToBase64.convertToBase64(bytes));
}
TLDR
- Send the byte to the native OS
- Convert and return to react-native.
PS: Do comment below if further explanation is required, I am happy to oblige!.
Cheers (:
External links
- Twitter: @benny_wayn
- LinkedIn: Ibenge-uforo
- Ongoing Research: Social Survey
Top comments (0)