【Android】Glideを使って画像をBitmapとしてダウンロード・取得する方法
本記事では、Androidアプリ開発で広く使われている画像読み込みライブラリ「Glide」を利用して、URLから画像をダウンロードし、Bitmapとして取得する方法を解説します。GlideのasBitmap()メソッドとCustomTargetを組み合わせることで、画像を簡単にBitmap形式で受け取ることができます。
手順1:プロジェクトの作成と依存関係の追加
まず、Android Studioで新規プロジェクトを作成します。メニューから「File」⇒「New Project」を選択し、必要な項目を入力してプロジェクトを作成しましょう。
続いて、appレベルのbuild.gradle(Module: app)に以下の依存関係を追加します。
implementation 'com.github.bumptech.glide:glide:4.9.0'
手順2:レイアウトファイル(activity_main.xml)の編集
res/layout/activity_main.xmlに以下のコードを追加します。ここでは、ダウンロードした画像を画面全体に表示するためのImageViewを配置しています。
<?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"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>手順3:MainActivity.javaの実装
src/MainActivity.javaに以下のコードを記述します。ポイントは、Glide.with(this).asBitmap()でBitmapとして画像を読み込み、CustomTarget<Bitmap>のonResourceReady()コールバック内で取得したBitmapをImageViewにセットしている点です。
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
Glide.with(this).asBitmap().load("https://www.google.es/images/srpr/logo11w.png").into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
}手順4:AndroidManifest.xmlへの権限追加
インターネットから画像をダウンロードするため、androidManifest.xmlにINTERNET権限を追加します。この記述を忘れると画像の読み込みに失敗するので注意してください。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
<uses-permission android:name="android.permission.INTERNET"/>
<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端末をPCに接続した状態で、Android Studioでプロジェクト内のアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックします。実行デバイスとして自分のスマートフォンを選択すると、アプリが起動し、ダウンロードされた画像が画面に表示されます。

まとめ
Glideを使えば、わずか数行のコードでURLから画像をダウンロードし、Bitmapとして扱うことができます。CustomTargetを利用することで、取得したBitmapをImageViewへの表示だけでなく、保存や加工などさまざまな用途に活用できます。ぜひ本記事の手順を参考に、実装してみてください。
-
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