base64 encode UTF-8 string - Android File Input Output

Android examples for File Input Output:Base64

Description

base64 encode UTF-8 string

Demo Code


//package com.java2s;
import android.graphics.Bitmap;

import android.util.Base64;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

public class Main {
    public static String encode(String s) {
        byte[] data;
        String base64 = "";
        try {//w w w  . j  ava 2s  .c o  m
            data = s.getBytes("UTF-8");
            base64 = Base64.encodeToString(data, Base64.DEFAULT);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return base64;
    }

    public static String encode(byte[] data)
            throws UnsupportedEncodingException {
        String base64 = Base64.encodeToString(data, Base64.DEFAULT);
        return base64;
    }

    public static String encode(Bitmap bitmap)
            throws UnsupportedEncodingException {
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArray);
        String base64 = Base64.encodeToString(byteArray.toByteArray(),
                Base64.DEFAULT);
        return base64;
    }
}

Related Tutorials