Android Picassoライブラリで画像をダウンロード・表示する方法を徹底解説
Picassoは、Square社が提供するAndroid向けの定番画像読み込みライブラリです。URLを指定するだけで、画像のダウンロードからキャッシュ管理、画面への表示まで自動的に処理してくれるため、ネットワーク通信のコードを自前で書く必要がありません。本記事では、Picassoを使って画像をダウンロードし、ImageViewに表示する方法をステップごとに解説します。
ステップ1:プロジェクトの作成と依存関係の追加
まず、Android Studioで新しいプロジェクトを作成します。メニューバーから「File」→「New Project」を選択し、必要な項目を入力してプロジェクトを作成してください。
続いて、build.gradle(Module: app)のdependenciesブロックに以下の依存関係を追加します。
implementation 'com.squareup.picasso:picasso:2.4.0'
追加後、「Sync Now」をクリックしてGradleの同期を行うことを忘れないようにしましょう。
ステップ2:レイアウトファイル(activity_main.xml)の作成
res/layout/activity_main.xmlに以下のコードを記述します。ここでは、画像を表示するためのImageViewと、ダウンロードを開始するButtonを配置しています。
<?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="4dp" tools:context=".MainActivity"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/btnDownload"/> <Button android:id="@+id/btnDownload" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAlignment="center" android:layout_alignParentBottom="true" android:text="Download Image"/> </RelativeLayout>
ステップ3:MainActivity.javaの実装
src/MainActivity.javaに以下のコードを記述します。ボタンがクリックされると、Picassoのload()メソッドに画像のURLを渡し、into()メソッドで対象のImageViewを指定するだけで、画像のダウンロードと表示が完了します。
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Button btnDownload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
btnDownload = findViewById(R.id.btnDownload);
btnDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Picasso.with(MainActivity.this)
.load("https://images.pexels.com/photos/1212487/pexels-photo1212487.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260")
.into(imageView);
}
});
}
}
ステップ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」アイコンをクリックしてください。デバイス選択ダイアログが表示されたら、接続中のモバイル端末を選択します。
アプリが起動すると初期画面が表示されます。「Download Image」ボタンをタップすると、指定したURLから画像がダウンロードされ、ImageViewに表示されます。

まとめ
Picassoを使えば、わずか数行のコードで画像のダウンロードと表示を実現できます。スレッド管理やキャッシュ処理をライブラリ側が担ってくれるため、開発者はUIの実装に集中できるのが大きなメリットです。なお、より新しいプロジェクトでは後継ライブラリであるGlideやCoilの採用も検討するとよいでしょう。
-
Androidで2つの画像をオーバーレイしてImageViewに表示する方法
このチュートリアルでは、Androidアプリで2つの画像を重ね合わせて(オーバーレイ)ImageViewに表示する方法を解説します。画像の重ね合わせには、複数のdrawableを積み重ねて描画できる「layer-list」というリソースを活用します。 ステップ1:Android Studioで新規プロジェクトを作成する Android Studioを起動し、「File」→「New Project」を選択します。必要な項目をすべて入力して、新しいプロジェクトを作成してください。 ステップ2:レイアウトファイル(activity_main.xml)を編集する res/layout/activity
-
AndroidでアニメーションGIF画像を表示する方法【Glideライブラリ活用ガイド】
Androidアプリでは、ImageViewにそのままGIFファイルを読み込んでもアニメーションは再生されません。GIFを動かして表示するには、画像読み込みライブラリ「Glide」を利用するのが最も簡単で定番の方法です。この記事では、Android Studioでプロジェクトを作成し、Glideを使ってアニメーションGIFを表示するまでの手順を、サンプルコード付きでわかりやすく解説します。Step 1:プロジェクトの作成とGlideの導入まず、Android Studioで新しいプロジェクトを作成します。メニューバーから「File → New Project」を選択し、必要な項目を入力してプロ