Monday, March 8, 2010

How to base64 encode decode Android

The official documentation states that base64 encode and decode in Android is in the Android.util package. But when you go to use it you will find it's not there!

After much searching, I confirmation Android left base64 encode and decode out of the Android.util package. So what to do? Don't worry, Robert W. Harder has but together a very fast Base64 encoding and decoding class you can include in your project. Here is how to add base64 encode and decode to your project.

Step 1. Visit Robert's website http://iharder.sourceforge.net/current/index.html and download the class file.

 
Step 2. The zip file you downloaded in step 1 includes the base64 class and a folder with examples and documentation.
Step 3. Copy the Base64.java class file into your project. Copy into your appropriate "src" folder.
Step 4. Refresh your project so the new file is included.
Step 5.When the Base64.java file is included in your project, you will get the following error "The declared package does not match the expected package". Fix this by opening the class file and adding a package declaration that matches your project.
Step 6. Done. You're ready to base64 encode and decode.
 

Download the complete tutorial here

17 comments:

AntonDerevyanko said...

Is this method works for big files? I need to encode files into base64 and send them to server. Base64.encode method doesn't work for files, bigger than 500kB!

Anonymous said...

Line 561, 'int rem = Math.min(3,raw.remaining());'

Method was not found in Math.

3m said...

Part of the API now:
http://developer.android.com/reference/android/util/Base64.html

Unknown said...

@3M, only part of the API for 2.2+ If you want to target below that API level you still need to create your own solution.

I also moved the zip file to another server. It can now be downloaded again.

Anonymous said...

I also need for bigger than 500k files?

Anonymous said...

I has the some problem and the problem is the Android don't suport a lot of memory..also you need do cute you file in bytes of 500kb and convert and send one at a time

Krishna Shetty said...

Strange the Android API(Android 2.2) "android.util.Base64.encodeToString(MyMsg, 0, MyMsg.length, android.util.Base64.DEFAULT);" does not work for me(I can't decode the encoded string properly.).

The mentioned code from Robert's website http://iharder.sourceforge.net/current/index.html works fine

Thank you.

KrushnaN said...

hai..Robert W. Harder

i need to convert my camera snaps into base 64 formate....how to do?

Anonymous said...

excellent tuto
thanks a lot

jo - ardianto said...

hello sir, can you give examples of projects that use base64 in parsing the image? I need a reference for my project.
thank you very much ..

Anonymous said...

I need the functionality of NO_WRAP that was added in Android API 8.

Jovan Jovanovic said...

Thank! Helped a lot! Base64 won't work in combination with sun.util...Base64, but with this one works perfectly! Thanks again :)

Anonymous said...

A lot of thanks!!!! :)

DomFilk said...

you can try this free online service to convert base64 to string online.

Blogger Pro said...

nice

yasmine said...

100% Free SEO Tools is a collection of best seo tools website: Plagiarism Checker, Article Rewriter, Backlink Checker, Online Ping, Keyword Checker
http://www.freeseotools.es/

yasmine said...

100% Free SEO Tools is a collection of best seo tools website: Plagiarism Checker, Article Rewriter, Backlink Checker, Online Ping, Keyword Checker
http://www.freeseotools.es/

Post a Comment

Note: Only a member of this blog may post a comment.