SnappFr / react-native-image-base64

Simple react native library to convert an image to a base64 string 🌄
MIT License
49 stars 28 forks source link

return wrong base64 string on android #19

Open maxweb4u opened 4 years ago

maxweb4u commented 4 years ago

Platform: android RN: 0.60.4

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

return wrong base64 string.

shehzadosama commented 4 years ago

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

thinhvkit commented 3 years ago

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