Pergunta

I'm trying to generate filenames in my Android app from a 4 byte byte array. I'm Base64 encoding the byte array with the URL_SAFE option. However, the generated string seems to end with a newline character, which makes it unusable as a filename. Is there anyway to remove the newline?

My code is as follows:

byte[] myByteArray = new byte[4];
myByteArray = generateBytes(myByteArray); // fills the byte array with some data
final String byteString = Base64.encodeToString(myByteArray, Base64.URL_SAFE);

After some googling, I found out that in Android, Base64 encoding automatically inserts a newline after the string, and that using the NO_WRAP flag would solve this. However, is the NO_WRAP flag generated output filename safe?

Thanks.

Foi útil?

Solução

OK, turns out I can use (Base64.URL_SAFE | Base64.NO_WRAP) to apply both flags.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top