Android examples for android.util:Base64
Encode image to Base64 string
import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Locale; import java.util.Vector; import java.util.regex.Pattern; import android.util.Base64; public class Main { public static String Image2Base64(String imageName) { try {/*from w w w . j a v a 2 s . c o m*/ FileInputStream in = new FileInputStream(imageName); byte buffer[] = InputStram2byteArray(in); byte[] encode = Base64.encode(buffer, Base64.DEFAULT); return new String(encode); } catch (Exception e) { e.printStackTrace(); return ""; } } public static byte[] InputStram2byteArray(InputStream in) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); if (in != null) { byte[] buffer = new byte[1024]; int length = 0; while ((length = in.read(buffer)) != -1) { out.write(buffer, 0, length); } out.close(); in.close(); return out.toByteArray(); } } catch (Exception e) { e.printStackTrace(); } return null; } }