前提・実現したいこと
Glide(画像ダウンローダーライブラリ)を使って画像をbitmapで取得し、システム壁紙に設定したいです。
発生している問題・エラーメッセージ
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.wallpaper, PID: 7803 android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565) at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:424) at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:564) at okio.InputStreamSource.read(JvmOkio.kt:90) at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:129) at okio.RealBufferedSource.request(RealBufferedSource.kt:206) at okio.RealBufferedSource.require(RealBufferedSource.kt:199) at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.kt:381) at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:429) at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408) at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:276) at okio.RealBufferedSource.read(RealBufferedSource.kt:189) at okio.RealBufferedSource.exhausted(RealBufferedSource.kt:197) at okio.InflaterSource.refill(InflaterSource.kt:112) at okio.InflaterSource.readOrInflate(InflaterSource.kt:76) at okio.InflaterSource.read(InflaterSource.kt:49) at okio.GzipSource.read(GzipSource.kt:69) at okio.Buffer.writeAll(Buffer.kt:1642) at okio.RealBufferedSource.readString(RealBufferedSource.kt:95) at okhttp3.ResponseBody.string(ResponseBody.kt:187) at com.example.wallpaper.MainActivity$onCreate$2$1.onResponse$lambda-1(MainActivity.kt:55) at com.example.wallpaper.MainActivity$onCreate$2$1.$r8$lambda$L-f6eyl8kI29t2zf84oLw39ndx8(Unknown Source:0) at com.example.wallpaper.MainActivity$onCreate$2$1$$ExternalSyntheticLambda1.run(Unknown Source:6) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:8125) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100) Suppressed: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565) at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:424) at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:564) at okio.InputStreamSource.read(JvmOkio.kt:90) at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:129) at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:427) at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:320) at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:105) at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:426) at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408) at okhttp3.internal.Util.skipAll(Util.kt:337) at okhttp3.internal.Util.discard(Util.kt:358) at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.close(Http1ExchangeCodec.kt:450) at okio.ForwardingSource.close(ForwardingSource.kt:34) at okhttp3.internal.connection.Exchange$ResponseBodySource.close(Exchange.kt:309) at okio.RealBufferedSource.close(RealBufferedSource.kt:477) at okio.RealBufferedSource.close(RealBufferedSource.kt:477) at okio.InflaterSource.close(InflaterSource.kt:136) at okio.GzipSource.close(GzipSource.kt:171) at okio.RealBufferedSource.close(RealBufferedSource.kt:477) at kotlin.io.CloseableKt.closeFinally(Closeable.kt:60) at okhttp3.ResponseBody.string(ResponseBody.kt:186)
該当のソースコード
Kotlin
1 Glide.with(this@MainActivity) 2 .asBitmap() 3 .load(photoURL) 4 .into(object : CustomTarget<Bitmap>(){ 5 override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) { 6 wm.setBitmap(resource) 7 } 8 9 override fun onLoadCleared(placeholder: Drawable?) { 10 11 } 12 }) 13 }catch(e:JSONException) { 14 15 } 16 }) 17 } 18 }) 19 }
AndroidManifest.xml(権限部分のみ)
xml
1<uses-permission android:name="android.permission.SET_WALLPAPER" /> 2<uses-permission android:name="android.permission.INTERNET" />
試したこと
into()でImageViewに表示させると正常に表示されるのでおそらくGlideの使い方が間違ってます。
補足情報(FW/ツールのバージョンなど)
Gradleの記述です。
implementation 'com.squareup.okhttp3:okhttp:4.9.0' implementation 'com.github.bumptech.glide:glide:4.11.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
ターゲットSDKと最小SDKです
minSdk 25 targetSdk 30
Android Studio 2020.3.1
回答1件
あなたの回答
tips
プレビュー