react-native-image-base64 icon indicating copy to clipboard operation
react-native-image-base64 copied to clipboard

return wrong base64 string on android

Open maxweb4u opened this issue 5 years ago • 2 comments

Platform: android RN: 0.60.4

ImgToBase64.getBase64String("file:///data/user/0/com.myapp/cache/1581517067881.JPEG")

return wrong base64 string.

maxweb4u avatar Feb 12 '20 14:02 maxweb4u

Hi @maxweb4u, Basically it returns the image base64 with adding (line breaks i.e. \n) implicitly. So that it differs with the actual base64 and you thought it is returning wrong base64. So, I have work arround and finds the solution to removes the line breaks from it. e.g: let resizedImgBase64 = await ImgToBase64.getBase64String(resizedImg.uri) resizedImgBase64 = resizedImgBase64.replace(/\n/gi, "");

We are using replace method of javascript here for removing \n's from base64 string. We are replacing all the \n with empty character "". Hope it works for you. Thanks

shehzadosama avatar Mar 10 '20 12:03 shehzadosama

RNImgToBase64Module.java private String bitmapToBase64(Bitmap bitmap) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); return Base64.encodeToString(byteArray, Base64.DEFAULT); } we need to change Base64.DEFAULT to Base64.NO_WRAP

thinhvkit avatar Jan 27 '21 07:01 thinhvkit