encode Object to base64 String - Android File Input Output

Android examples for File Input Output:Object Serialization

Description

encode Object to base64 String

Demo Code


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Base64;
import android.util.Log;
import java.io.*;

public class Main{
    private static final String TAG = LogUtils
            .makeLogTag(EncodeUtils.class);
    public static String encodeObject(Object object) {
        if (object == null) {
            return "";
        }/*w  ww  .j a v  a2  s .  c  om*/

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        }
        String base64 = Base64.encodeToString(baos.toByteArray(),
                Base64.DEFAULT);
        try {
            baos.close();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return base64;
    }
}

Related Tutorials