-2

I was trying to remove line breaks, but with this code below I can't proceed to remove it with replace() method.

private String encryptBase64(String data){
    byte[] values = null;

    try{
        values = data.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    String encrypted = Base64.encodeToString(values, Base64.DEFAULT);
        encrypted.replace("\r\n", "");
    Log.i("base64", encrypted);
    return encrypted;
}

As the code above, it's clear that I tried to remove line breaks but the code failed to do it. How to remove the line breaks?

Screenshot : base64 Log

6
  • 1
    Because strings are immutable - you're calling replace but ignoring the return value. That's got nothing to do with Android or base64 - it's just how Java strings work. Commented Jun 27, 2017 at 6:43
  • I did replace and not replace the string. There's no differences.. Commented Jun 27, 2017 at 6:45
  • lol. I did that before, but doesn't solve my issue. I though it was different case, so I made a question here. Commented Jun 27, 2017 at 7:07
  • I DID post my "reasonable" code that doesn't work. Have you tried by yourself? Commented Jun 27, 2017 at 7:13
  • sometimes, its look same but its different. Think again before you talk. Commented Jun 27, 2017 at 7:20

3 Answers 3

2

I have solution for your problem , You have to use URLEncoder to encode such string to pass in url. Example :

private String encryptBase64(String data){
byte[] values = null;
try{
    values = data.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}
String encrypted = Base64.encodeToString(values, Base64.DEFAULT);
// encrypted.replace("\r\n", "");
Log.i("base64", encrypted);
// =========== use this line of code ===============
    try {
            url_encode_val = URLEncoder.encode(encrypted, "utf-8");
            Log.e("encryptData_To url::", url_encode_val);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

return url_encode_val;
}
Sign up to request clarification or add additional context in comments.

2 Comments

its work! wow. I'm impressed.. Thanks a lot Amit, god bless you
i'm happy it was helpful to you .. happy coding :)
0

Just replace encrypted = encrypted.replaceAll("(\\r|\\n|\\r\\n)+", ""); instead of encrypted.replace("\n", "");

7 Comments

nothing happens
you try with replaceAll function?
i did, but still same
@KevinBradley try with editted answer.
same thing, thanks for helping me. I think I need to close this question.
|
0

What you can do is:

String stringAsProfImg = android.util.Base64.encodeToString(byteArrayOfImg, android.util.Base64.DEFAULT);

Or what I am doing is converting a bitmap image to byte array and then converting byte array to BASE64 String.

Below is an example:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byteArrayOfImg = stream.toByteArray();
                bitmapImage.recycle();
                bitmapImage = null;
                stringAsProfImg =android.util.Base64.encodeToString(byteArrayOfImg, android.util.Base64.DEFAULT);

1 Comment

compress to JPEG? you god damn right

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.