Androidで文字列をデコードする方法【Base64を使ったサンプルコード付き】
この記事では、Androidアプリで文字列をデコード(復元)する方法を、Base64を使った具体的なサンプルコードとともに解説します。エンコード処理もあわせて実装するため、データの変換・復元の流れをまるごと理解できる内容になっています。
ステップ1:Android Studioで新規プロジェクトを作成する
まず、Android Studioを開き、メニューから「File」→「New Project」を選択して新しいプロジェクトを作成します。アプリ名やパッケージ名など、必要な項目をすべて入力してプロジェクトの準備を整えましょう。
ステップ2:レイアウトファイル(activity_main.xml)を作成する
次に、res/layout/activity_main.xml に以下のコードを記述します。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" android:layout_marginTop="30dp" tools:context=".MainActivity"> <EditText android:id="@+id/edit_query" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonPanel" android:text="Check" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
上記のレイアウトでは、EditText(テキスト入力欄)、Button(チェック用ボタン)、そして結果を表示するTextViewを2つ配置しています。ユーザーがEditTextに文字列を入力してボタンをタップすると、その文字列に対してエンコードとデコードが順番に実行される仕組みです。
ステップ3:MainActivity.javaにロジックを実装する
続いて、src/MainActivity.java に以下のコードを追加します。
package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.nio.charset.StandardCharsets;
public class MainActivity extends AppCompatActivity {
TextView text,text1;
EditText edit_query;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_query = findViewById(R.id.edit_query);
text = findViewById(R.id.text);
text1 = findViewById(R.id.text1);
findViewById(R.id.buttonPanel).setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
if (!edit_query.getText().toString().isEmpty()) {
String encode = Base64.encodeToString(edit_query.getText().toString().getBytes(), Base64.DEFAULT);
text.setText(encode);
byte[] data = Base64.decode(encode, Base64.DEFAULT);
String text = new String(data, StandardCharsets.UTF_8);
text1.setText(text);
}
}
});
}
}コードのポイント
- Base64.encodeToString():EditTextに入力された文字列をバイト配列に変換し、Base64形式でエンコードします。
- Base64.decode():エンコード済みの文字列を、元のバイト配列へとデコードします。
- new String(data, StandardCharsets.UTF_8):デコードしたバイト配列をUTF-8エンコーディングの文字列として復元し、画面に表示します。
また、入力欄が空の場合に処理を行わないよう isEmpty() によるチェックも行っている点は、実務でも役立つ防御的な書き方です。
アプリを実行して動作を確認しよう
それでは、実際にアプリを実行してみましょう。ここでは、Android端末を実機としてPCに接続しているものとします。Android Studioでプロジェクト内の任意のアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックしてください。接続中のモバイル端末を選択すると、端末側にアプリの初期画面が表示されます。

上記の実行結果のように、入力された文字列がまずBase64形式でエンコードされ、その後に正しくデコードされて元の文字列が画面に表示されます。このように android.util.Base64 クラスを使えば、Androidアプリでの文字列のエンコード/デコード処理をシンプルに実装できます。
-
【Android】セカンドアクティビティからメインアクティビティへデータを返す方法を解説
このチュートリアルでは、Androidアプリにおいてセカンドアクティビティからメインアクティビティへデータ(処理結果)を返す方法を、サンプルコードを交えて解説します。2つの数値を入力して別画面へ渡し、その画面での計算結果を元の画面に表示させるアプリを例に、データ受け渡しの基本を学びましょう。ポイントは startActivityForResult() と onActivityResult() の2つです。 ステップ1:新規プロジェクトを作成する Android Studioを起動し、「File」→「New Project」を選択して、必要な項目を入力して新しいプロジェクトを作成します。 ス
-
Android端末のメインメールアドレスを取得する方法をサンプルコード付きで解説
本記事では、Android端末に登録されているメインのメールアドレスを取得する方法を、実際のサンプルコードとともにわかりやすく解説します。AccountManagerとGET_ACCOUNTS権限を活用することで、端末に設定されたアカウント情報からメールアドレスを取得できます。 手順1:Android Studioで新規プロジェクトを作成する Android Studioを起動し、「File」→「New Project」を選択して、必要な項目を入力して新しいプロジェクトを作成します。 手順2:res/layout/activity_main.xml にコードを追加する 以下のコードをレイアウト