Iam using base64 encoding for authentication to contact a dot net server and for image decoding. what library should i include for this. iam bit confused here. can someone help me
3 Answers
Android has inbuilt library for Base64, check following link
http://developer.android.com/reference/android/util/Base64.html
It is available from API version 8. If your application supports lower than API 8 then you can use following library http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html
OR
you can use official Android Open Source code. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/util/Base64.java

- 1
- 1

- 2,962
- 19
- 27
There are various libraries you can use, including Apache Commons Codec. Personally though, I prefer the API of this public domain implementation, which is also pretty speed (which I guess may be important on a mobile device). That's also a single Java file, which makes it pretty easy to integrate. The same page lists some other options too.
EDIT: Of course, if you can use the built-in one, that's even easier still, in terms of integration :)

- 1,421,763
- 867
- 9,128
- 9,194
-
Thanks. i will try the api you specified. – AD14 Jun 14 '11 at 09:11
-
1There is a problem though with Apache Commons Codec on Android - Base64.decodeBase64 cannot be found, see here http://stackoverflow.com/questions/5147789/nosuchmethoderror-using-commonc-codec-in-android-application . – Mike Bevz Jan 13 '12 at 13:26
-
is there any method in Base64 lib, that can check the string is in encoded or not? – Asad Mukhtar May 02 '19 at 07:33
-
@AsadMukhtar: I don't understand what you're asking. Every string that's a valid base64-encoded representation of some binary data is also valid as a perfectly ordinary string. For example, the string "TEST" is valid base64, but it's also valid in itself. You should try hard to avoid getting into the situation where any given string may or may not be base64 data. – Jon Skeet May 02 '19 at 07:54
-
@JonSkeet i need a check that the string coming from server is encoded or not ? I have gone through different solutions on stackoverflow and found the best rated answer is that regex "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$" but that regex also gives me the error like it is good for all other strings like Test, hello world, but when the string is nott coming from server it is not encoded string but that method try to decode it, that is the issue – Asad Mukhtar May 02 '19 at 08:05
-
@AsadMukhtar: As I said, every valid base64 string is also a valid "normal" string. If you receive "TEST" from the server, how would you expect to know whether that's intended to be "TEST" as a string, or a base64 representation of the binary data { 0x4C, 0x44, 0x93 }? It's the same string either way... But all of this is off-topic for *this* question. You should ask a new question with more context. – Jon Skeet May 02 '19 at 08:25
If your minimal requirement is Android 2.2 or later, then you can use android.util.Base64
class.

- 53,859
- 22
- 133
- 139