LoginSignup
4
0

More than 5 years have passed since last update.

Glide v4 の通信ライブラリをOkHttpに置き換えてデバッグログを表示する

Last updated at Posted at 2018-05-02

問題

  • 大きいサイズの画像表示が遅いので原因を調べたい。
  • Android Profilerで通信部分を確認できるが、通信ライブラリのログで詳細を確認したい。
  • Glideの通信ログを出力できないか調べました。

解決案

  • 通信ライブラリをOkHttpに置き換えてログを出力するように設定。

環境

  • Android Studio 3.1
  • Glide 4.7.1
  • OkHttp 3.9.1

Glideとは

  • Androidの画像ライブラリ。サーバ上の画像をアプリに表示することができる
  • キャッシュも自動的に保存・管理してくれるので便利
  • 公式サイト

GlideとOkHttpをインストール

app/build.gradle

def glideVersion = "4.7.1"
implementation "com.github.bumptech.glide:glide:$glideVersion"
annotationProcessor "com.github.bumptech.glide:compiler:$glideVersion"
implementation "com.github.bumptech.glide:okhttp3-integration:$glideVersion"

def okhttpVersion = "3.9.1"
implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttpVersion"

Custom Moduleの実装

MyAppGlideModule
@GlideModule
public class MyAppGlideModule extends AppGlideModule {
    @Override
    public void registerComponents(Context context, Glide glide, Registry registry) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            builder.addInterceptor(interceptor);
        }
        OkHttpClient client = builder.build();
        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
    }
}

Glideでの画像読み込み

HogeActivity
GlideApp.with(this).load(imgUrl).into(imageView);

動作確認

  • LogcatにOkHttpのタグで画像URLやサーバとのやりとりが出力されていることが確認できると思います。
  • (一度読み込むとキャッシュして通信が発生しないので注意してください)

終わりに

  • Glideは細かい設定が他にもあるので少しずつ使いこなしていきたいと思います。

参考記事

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0