Androidで画像をBase64文字列に変換する方法【サンプルコード付きで解説】
はじめに
この記事では、Androidアプリで画像(Bitmap)をBase64形式の文字列に変換する方法を、実際のサンプルコードとともにステップごとに解説します。Base64エンコードは、画像データをテキストとしてAPIサーバーへ送信したり、データベースに保存したりする際に広く利用される技術です。
手順1:新しいプロジェクトを作成する
Android Studioを開き、メニューから「File」→「New Project」を選択します。必要な項目をすべて入力して、新しいプロジェクトを作成しましょう。
手順2:レイアウトファイル(activity_main.xml)を編集する
res/layout/activity_main.xml に以下のコードを追加します。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:padding="8dp" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:textSize="12sp" android:textStyle="bold" /> </RelativeLayout>
手順3:MainActivity.java に変換処理を実装する
src/MainActivity.java に以下のコードを記述します。
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
TextView textView;
@SuppressLint("WrongThread")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.instaimage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
textView.setText(imageString);
}
}
このコードの処理の流れは以下のとおりです。
- リソースフォルダ内の画像(instaimage)をBitmapFactoryで読み込み、Bitmapオブジェクトとして取得します。
- ByteArrayOutputStreamを使い、BitmapをJPEG形式(品質100)で圧縮してバイト配列に変換します。
- android.util.Base64クラスのencodeToStringメソッドでバイト配列をBase64文字列にエンコードします。
- 変換結果の文字列をTextViewに表示します。
手順4:AndroidManifest.xml を設定する
androidManifest.xml には以下のコードを記述します。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
アプリを実行して動作を確認しよう
それでは、アプリケーションを実行してみましょう。ここでは、実機のAndroidスマートフォンをパソコンに接続しているものとします。Android Studioからアプリを実行するには、プロジェクト内のアクティビティファイルを開き、ツールバーにある実行
アイコンをクリックします。表示された選択肢から接続したモバイルデバイスを選択すると、スマートフォン側の画面に実行結果が表示されます。

-
AndroidでDrawableをBitmapに変換する方法を徹底解説!サンプルコード付き
この記事では、Androidアプリ開発においてDrawableをBitmap(ビットマップ)に変換する方法を、実際に動作するサンプルコードとともに解説します。画像の加工・保存・送信などを行いたい場合、Bitmap形式への変換は頻繁に使われるテクニックなので、ぜひマスターしておきましょう。 手順1:Android Studioで新規プロジェクトを作成する Android Studioを起動し、「File」⇒「New Project」を選択して新しいプロジェクトを作成します。プロジェクト名やパッケージ名など、必要な項目をすべて入力してください。テンプレートは「Empty Activity」で問題あ
-
【Android】Glideを使って画像をBitmapとしてダウンロード・取得する方法
本記事では、Androidアプリ開発で広く使われている画像読み込みライブラリ「Glide」を利用して、URLから画像をダウンロードし、Bitmapとして取得する方法を解説します。GlideのasBitmap()メソッドとCustomTargetを組み合わせることで、画像を簡単にBitmap形式で受け取ることができます。手順1:プロジェクトの作成と依存関係の追加まず、Android Studioで新規プロジェクトを作成します。メニューから「File」⇒「New Project」を選択し、必要な項目を入力してプロジェクトを作成しましょう。続いて、appレベルのbuild.gradle(Module