I want to check if a string is in Base64 encoded format or not in an android app. If it is in Base64 then i need to decode it and display the content after decoding. I have used the answer of Base 64 encode and decode example code and How to check whether the string is base64 encoded or not but the problem is how to compare/validate a Base64-regex with the string i got after reading a nfc tag. Thanks!
Asked
Active
Viewed 8,009 times
2
-
3This might be hepful to you http://stackoverflow.com/questions/8571501/how-to-check-whether-the-string-is-base64-encoded-or-not – Keyur Lakhani Aug 24 '15 at 12:26
-
Yes i have seen the question but how to do exactly the same in android because we can not do like var isBase64Valid = base64Rejex.test(base64Data); in android. – Swapnil Lanjewar Aug 24 '15 at 12:54
2 Answers
0
It might be late to answer this question but if you are till looking for this, then this might be helpfull.
if (org.apache.commons.codec.binary.Base64.isArrayByteBase64(base64String.getBytes())) {
try{
chatTitle = new String(Base64.decode(base64String, Base64.DEFAULT), "UTF-8");
} catch (Exception e) {}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
The org.apache.commons.codec.binary.Base64
comes default with Android

Rivu Chakraborty
- 1,372
- 2
- 12
- 37
0
try
private boolean IsBase64Encoded(String value)
{
try {
byte[] decodedString = Base64.decode(value, Base64.DEFAULT);
BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return true;
} catch (Exception e) {
return false;
}
}

Sỹ Phạm
- 530
- 6
- 16