Base64 Encode and Decode using Java (Android)

Base64 class

package com.example.app;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String text = "Hello";
        byte[] data = text.getBytes();
        String base64Str = Base64.encodeToString(data, Base64.DEFAULT);
        Log.d("MY_APP", base64Str);

        data = Base64.decode(base64Str, Base64.DEFAULT);
        text = new String(data);
        Log.d("MY_APP", text);
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.