質問編集履歴
12
コードの修正
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -1,6 +1,5 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ### 前提・実現したいこと
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
            [Okhttp](https://square.github.io/okhttp/)でJSONを取得してそこからURLを抜き出して、
         
     | 
| 
       4 
3 
     | 
    
         
             
            [Glide](https://bumptech.github.io/glide/doc/getting-started.html)(画像ダウンローダーライブラリ)を使って画像をbitmapで取得し、システム壁紙に設定したいです。
         
     | 
| 
       5 
4 
     | 
    
         | 
| 
       6 
5 
     | 
    
         
             
            ### 発生している問題・エラーメッセージ
         
     | 
| 
         @@ -66,40 +65,6 @@ 
     | 
|
| 
       66 
65 
     | 
    
         
             
            ### 該当のソースコード
         
     | 
| 
       67 
66 
     | 
    
         | 
| 
       68 
67 
     | 
    
         
             
            ```Kotlin
         
     | 
| 
       69 
     | 
    
         
            -
            import android.app.WallpaperManager
         
     | 
| 
       70 
     | 
    
         
            -
            import android.content.Intent
         
     | 
| 
       71 
     | 
    
         
            -
            import android.net.Uri
         
     | 
| 
       72 
     | 
    
         
            -
            import android.os.Bundle
         
     | 
| 
       73 
     | 
    
         
            -
            import android.os.Handler
         
     | 
| 
       74 
     | 
    
         
            -
            import android.widget.Button
         
     | 
| 
       75 
     | 
    
         
            -
            import androidx.appcompat.app.AppCompatActivity
         
     | 
| 
       76 
     | 
    
         
            -
            import okhttp3.*
         
     | 
| 
       77 
     | 
    
         
            -
            import org.json.JSONObject
         
     | 
| 
       78 
     | 
    
         
            -
            import java.io.IOException
         
     | 
| 
       79 
     | 
    
         
            -
            import android.os.Looper
         
     | 
| 
       80 
     | 
    
         
            -
            import android.widget.ImageView
         
     | 
| 
       81 
     | 
    
         
            -
            import com.bumptech.glide.Glide
         
     | 
| 
       82 
     | 
    
         
            -
            import org.json.JSONException
         
     | 
| 
       83 
     | 
    
         
            -
             
     | 
| 
       84 
     | 
    
         
            -
            class MainActivity : AppCompatActivity() {
         
     | 
| 
       85 
     | 
    
         
            -
                override fun onCreate(savedInstanceState: Bundle?) {
         
     | 
| 
       86 
     | 
    
         
            -
                    super.onCreate(savedInstanceState)
         
     | 
| 
       87 
     | 
    
         
            -
                    setContentView(R.layout.activity_main)
         
     | 
| 
       88 
     | 
    
         
            -
                    
         
     | 
| 
       89 
     | 
    
         
            -
                    val setbtn = findViewById<Button>(R.id.setting) 
         
     | 
| 
       90 
     | 
    
         
            -
                    val wm = WallpaperManager.getInstance(this)
         
     | 
| 
       91 
     | 
    
         
            -
                    setbtn.setOnClickListener{ //ボタンが押されたら
         
     | 
| 
       92 
     | 
    
         
            -
                       val client = OkHttpClient()
         
     | 
| 
       93 
     | 
    
         
            -
                       val request = Request.Builder()
         
     | 
| 
       94 
     | 
    
         
            -
                            .url("JSONURL")//JSONのURLが入っているものとします
         
     | 
| 
       95 
     | 
    
         
            -
                            .build()
         
     | 
| 
       96 
     | 
    
         
            -
                        client.newCall(request).enqueue(object : Callback {
         
     | 
| 
       97 
     | 
    
         
            -
                            val mainHandler: Handler = Handler(Looper.getMainLooper())
         
     | 
| 
       98 
     | 
    
         
            -
                            override fun onResponse(call: Call, response: Response) {
         
     | 
| 
       99 
     | 
    
         
            -
                                val json = JSONObject(response.body!!.string())
         
     | 
| 
       100 
     | 
    
         
            -
                                mainHandler.post(Runnable(){
         
     | 
| 
       101 
     | 
    
         
            -
                                    try{
         
     | 
| 
       102 
     | 
    
         
            -
                                        val photoURL = json.getString("url")
         
     | 
| 
       103 
68 
     | 
    
         
             
                                        Glide.with(this@MainActivity)
         
     | 
| 
       104 
69 
     | 
    
         
             
                                            .asBitmap()
         
     | 
| 
       105 
70 
     | 
    
         
             
                                            .load(photoURL)
         
     | 
| 
         @@ -128,8 +93,7 @@ 
     | 
|
| 
       128 
93 
     | 
    
         
             
            ```
         
     | 
| 
       129 
94 
     | 
    
         
             
            ### 試したこと
         
     | 
| 
       130 
95 
     | 
    
         | 
| 
       131 
     | 
    
         
            -
             
     | 
| 
      
 96 
     | 
    
         
            +
            into()でImageViewに表示させると正常に表示されるのでおそらくGlideの使い方が間違ってます。
         
     | 
| 
       132 
     | 
    
         
            -
            JSONの取得が何故か出来なくなりました。JSONにはブラウザでアクセスできるので、URLが誤りや切れているなどはあり得ないと思います。
         
     | 
| 
       133 
97 
     | 
    
         | 
| 
       134 
98 
     | 
    
         
             
            ### 補足情報(FW/ツールのバージョンなど)
         
     | 
| 
       135 
99 
     | 
    
         | 
11
コードの修正
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -7,26 +7,6 @@ 
     | 
|
| 
       7 
7 
     | 
    
         | 
| 
       8 
8 
     | 
    
         
             
            ```
         
     | 
| 
       9 
9 
     | 
    
         
             
            E/AndroidRuntime: FATAL EXCEPTION: main
         
     | 
| 
       10 
     | 
    
         
            -
                Process: com.example.wallpaper, PID: 26599
         
     | 
| 
       11 
     | 
    
         
            -
                java.lang.IllegalArgumentException: You must call this method on a background thread
         
     | 
| 
       12 
     | 
    
         
            -
                    at com.bumptech.glide.util.Util.assertBackgroundThread(Util.java:150)
         
     | 
| 
       13 
     | 
    
         
            -
                    at com.bumptech.glide.request.RequestFutureTarget.doGet(RequestFutureTarget.java:192)
         
     | 
| 
       14 
     | 
    
         
            -
                    at com.bumptech.glide.request.RequestFutureTarget.get(RequestFutureTarget.java:130)
         
     | 
| 
       15 
     | 
    
         
            -
                    at com.example.wallpaper.MainActivity$onCreate$2$1.onResponse$lambda-1(MainActivity.kt:59)
         
     | 
| 
       16 
     | 
    
         
            -
                    at com.example.wallpaper.MainActivity$onCreate$2$1.$r8$lambda$IRlHFeXyK0M094pRmXljRq8c7HQ(Unknown Source:0)
         
     | 
| 
       17 
     | 
    
         
            -
                    at com.example.wallpaper.MainActivity$onCreate$2$1$$ExternalSyntheticLambda1.run(Unknown Source:6)
         
     | 
| 
       18 
     | 
    
         
            -
                    at android.os.Handler.handleCallback(Handler.java:883)
         
     | 
| 
       19 
     | 
    
         
            -
                    at android.os.Handler.dispatchMessage(Handler.java:100)
         
     | 
| 
       20 
     | 
    
         
            -
                    at android.os.Looper.loop(Looper.java:237)
         
     | 
| 
       21 
     | 
    
         
            -
                    at android.app.ActivityThread.main(ActivityThread.java:8125)
         
     | 
| 
       22 
     | 
    
         
            -
                    at java.lang.reflect.Method.invoke(Native Method)
         
     | 
| 
       23 
     | 
    
         
            -
                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
         
     | 
| 
       24 
     | 
    
         
            -
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
         
     | 
| 
       25 
     | 
    
         
            -
            ```
         
     | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
       27 
     | 
    
         
            -
            修正後エラー
         
     | 
| 
       28 
     | 
    
         
            -
            ```
         
     | 
| 
       29 
     | 
    
         
            -
            E/AndroidRuntime: FATAL EXCEPTION: main
         
     | 
| 
       30 
10 
     | 
    
         
             
                Process: com.example.wallpaper, PID: 7803
         
     | 
| 
       31 
11 
     | 
    
         
             
                android.os.NetworkOnMainThreadException
         
     | 
| 
       32 
12 
     | 
    
         
             
                    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
         
     | 
10
コードの修正
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ### 前提・実現したいこと
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
            [Okhttp](https://square.github.io/okhttp/)でJSONを取得してそ 
     | 
| 
      
 3 
     | 
    
         
            +
            [Okhttp](https://square.github.io/okhttp/)でJSONを取得してそこからURLを抜き出して、
         
     | 
| 
       4 
     | 
    
         
            -
            [Glide](https://bumptech.github.io/glide/doc/getting-started.html)( 
     | 
| 
      
 4 
     | 
    
         
            +
            [Glide](https://bumptech.github.io/glide/doc/getting-started.html)(画像ダウンローダーライブラリ)を使って画像をbitmapで取得し、システム壁紙に設定したいです。
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
            ### 発生している問題・エラーメッセージ
         
     | 
| 
       7 
7 
     | 
    
         | 
| 
         @@ -148,7 +148,8 @@ 
     | 
|
| 
       148 
148 
     | 
    
         
             
            ```
         
     | 
| 
       149 
149 
     | 
    
         
             
            ### 試したこと
         
     | 
| 
       150 
150 
     | 
    
         | 
| 
       151 
     | 
    
         
            -
            into()でImageViewに表示させると正常に表示されるのでおそらくGlideの使い方が間違ってます。
         
     | 
| 
      
 151 
     | 
    
         
            +
            ~~into()でImageViewに表示させると正常に表示されるのでおそらくGlideの使い方が間違ってます。~~
         
     | 
| 
      
 152 
     | 
    
         
            +
            JSONの取得が何故か出来なくなりました。JSONにはブラウザでアクセスできるので、URLが誤りや切れているなどはあり得ないと思います。
         
     | 
| 
       152 
153 
     | 
    
         | 
| 
       153 
154 
     | 
    
         
             
            ### 補足情報(FW/ツールのバージョンなど)
         
     | 
| 
       154 
155 
     | 
    
         | 
9
コードの修正
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -120,7 +120,7 @@ 
     | 
|
| 
       120 
120 
     | 
    
         
             
                                mainHandler.post(Runnable(){
         
     | 
| 
       121 
121 
     | 
    
         
             
                                    try{
         
     | 
| 
       122 
122 
     | 
    
         
             
                                        val photoURL = json.getString("url")
         
     | 
| 
       123 
     | 
    
         
            -
                                         
     | 
| 
      
 123 
     | 
    
         
            +
                                        Glide.with(this@MainActivity)
         
     | 
| 
       124 
124 
     | 
    
         
             
                                            .asBitmap()
         
     | 
| 
       125 
125 
     | 
    
         
             
                                            .load(photoURL)
         
     | 
| 
       126 
126 
     | 
    
         
             
                                            .into(object : CustomTarget<Bitmap>(){
         
     | 
8
コードの修正
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -24,6 +24,65 @@ 
     | 
|
| 
       24 
24 
     | 
    
         
             
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
         
     | 
| 
       25 
25 
     | 
    
         
             
            ```
         
     | 
| 
       26 
26 
     | 
    
         | 
| 
      
 27 
     | 
    
         
            +
            修正後エラー
         
     | 
| 
      
 28 
     | 
    
         
            +
            ```
         
     | 
| 
      
 29 
     | 
    
         
            +
            E/AndroidRuntime: FATAL EXCEPTION: main
         
     | 
| 
      
 30 
     | 
    
         
            +
                Process: com.example.wallpaper, PID: 7803
         
     | 
| 
      
 31 
     | 
    
         
            +
                android.os.NetworkOnMainThreadException
         
     | 
| 
      
 32 
     | 
    
         
            +
                    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
         
     | 
| 
      
 33 
     | 
    
         
            +
                    at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:424)
         
     | 
| 
      
 34 
     | 
    
         
            +
                    at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:564)
         
     | 
| 
      
 35 
     | 
    
         
            +
                    at okio.InputStreamSource.read(JvmOkio.kt:90)
         
     | 
| 
      
 36 
     | 
    
         
            +
                    at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:129)
         
     | 
| 
      
 37 
     | 
    
         
            +
                    at okio.RealBufferedSource.request(RealBufferedSource.kt:206)
         
     | 
| 
      
 38 
     | 
    
         
            +
                    at okio.RealBufferedSource.require(RealBufferedSource.kt:199)
         
     | 
| 
      
 39 
     | 
    
         
            +
                    at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.kt:381)
         
     | 
| 
      
 40 
     | 
    
         
            +
                    at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:429)
         
     | 
| 
      
 41 
     | 
    
         
            +
                    at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408)
         
     | 
| 
      
 42 
     | 
    
         
            +
                    at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:276)
         
     | 
| 
      
 43 
     | 
    
         
            +
                    at okio.RealBufferedSource.read(RealBufferedSource.kt:189)
         
     | 
| 
      
 44 
     | 
    
         
            +
                    at okio.RealBufferedSource.exhausted(RealBufferedSource.kt:197)
         
     | 
| 
      
 45 
     | 
    
         
            +
                    at okio.InflaterSource.refill(InflaterSource.kt:112)
         
     | 
| 
      
 46 
     | 
    
         
            +
                    at okio.InflaterSource.readOrInflate(InflaterSource.kt:76)
         
     | 
| 
      
 47 
     | 
    
         
            +
                    at okio.InflaterSource.read(InflaterSource.kt:49)
         
     | 
| 
      
 48 
     | 
    
         
            +
                    at okio.GzipSource.read(GzipSource.kt:69)
         
     | 
| 
      
 49 
     | 
    
         
            +
                    at okio.Buffer.writeAll(Buffer.kt:1642)
         
     | 
| 
      
 50 
     | 
    
         
            +
                    at okio.RealBufferedSource.readString(RealBufferedSource.kt:95)
         
     | 
| 
      
 51 
     | 
    
         
            +
                    at okhttp3.ResponseBody.string(ResponseBody.kt:187)
         
     | 
| 
      
 52 
     | 
    
         
            +
                    at com.example.wallpaper.MainActivity$onCreate$2$1.onResponse$lambda-1(MainActivity.kt:55)
         
     | 
| 
      
 53 
     | 
    
         
            +
                    at com.example.wallpaper.MainActivity$onCreate$2$1.$r8$lambda$L-f6eyl8kI29t2zf84oLw39ndx8(Unknown Source:0)
         
     | 
| 
      
 54 
     | 
    
         
            +
                    at com.example.wallpaper.MainActivity$onCreate$2$1$$ExternalSyntheticLambda1.run(Unknown Source:6)
         
     | 
| 
      
 55 
     | 
    
         
            +
                    at android.os.Handler.handleCallback(Handler.java:883)
         
     | 
| 
      
 56 
     | 
    
         
            +
                    at android.os.Handler.dispatchMessage(Handler.java:100)
         
     | 
| 
      
 57 
     | 
    
         
            +
                    at android.os.Looper.loop(Looper.java:237)
         
     | 
| 
      
 58 
     | 
    
         
            +
                    at android.app.ActivityThread.main(ActivityThread.java:8125)
         
     | 
| 
      
 59 
     | 
    
         
            +
                    at java.lang.reflect.Method.invoke(Native Method)
         
     | 
| 
      
 60 
     | 
    
         
            +
                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
         
     | 
| 
      
 61 
     | 
    
         
            +
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
         
     | 
| 
      
 62 
     | 
    
         
            +
                	Suppressed: android.os.NetworkOnMainThreadException
         
     | 
| 
      
 63 
     | 
    
         
            +
                    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
         
     | 
| 
      
 64 
     | 
    
         
            +
                    at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:424)
         
     | 
| 
      
 65 
     | 
    
         
            +
                    at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:564)
         
     | 
| 
      
 66 
     | 
    
         
            +
                    at okio.InputStreamSource.read(JvmOkio.kt:90)
         
     | 
| 
      
 67 
     | 
    
         
            +
                    at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:129)
         
     | 
| 
      
 68 
     | 
    
         
            +
                    at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:427)
         
     | 
| 
      
 69 
     | 
    
         
            +
                    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:320)
         
     | 
| 
      
 70 
     | 
    
         
            +
                    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:105)
         
     | 
| 
      
 71 
     | 
    
         
            +
                    at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:426)
         
     | 
| 
      
 72 
     | 
    
         
            +
                    at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408)
         
     | 
| 
      
 73 
     | 
    
         
            +
                    at okhttp3.internal.Util.skipAll(Util.kt:337)
         
     | 
| 
      
 74 
     | 
    
         
            +
                    at okhttp3.internal.Util.discard(Util.kt:358)
         
     | 
| 
      
 75 
     | 
    
         
            +
                    at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.close(Http1ExchangeCodec.kt:450)
         
     | 
| 
      
 76 
     | 
    
         
            +
                    at okio.ForwardingSource.close(ForwardingSource.kt:34)
         
     | 
| 
      
 77 
     | 
    
         
            +
                    at okhttp3.internal.connection.Exchange$ResponseBodySource.close(Exchange.kt:309)
         
     | 
| 
      
 78 
     | 
    
         
            +
                    at okio.RealBufferedSource.close(RealBufferedSource.kt:477)
         
     | 
| 
      
 79 
     | 
    
         
            +
                    at okio.RealBufferedSource.close(RealBufferedSource.kt:477)
         
     | 
| 
      
 80 
     | 
    
         
            +
                    at okio.InflaterSource.close(InflaterSource.kt:136)
         
     | 
| 
      
 81 
     | 
    
         
            +
                    at okio.GzipSource.close(GzipSource.kt:171)
         
     | 
| 
      
 82 
     | 
    
         
            +
                    at okio.RealBufferedSource.close(RealBufferedSource.kt:477)
         
     | 
| 
      
 83 
     | 
    
         
            +
                    at kotlin.io.CloseableKt.closeFinally(Closeable.kt:60)
         
     | 
| 
      
 84 
     | 
    
         
            +
                    at okhttp3.ResponseBody.string(ResponseBody.kt:186)
         
     | 
| 
      
 85 
     | 
    
         
            +
            ```
         
     | 
| 
       27 
86 
     | 
    
         
             
            ### 該当のソースコード
         
     | 
| 
       28 
87 
     | 
    
         | 
| 
       29 
88 
     | 
    
         
             
            ```Kotlin
         
     | 
| 
         @@ -64,9 +123,15 @@ 
     | 
|
| 
       64 
123 
     | 
    
         
             
                                        val bitmap = Glide.with(this@MainActivity)
         
     | 
| 
       65 
124 
     | 
    
         
             
                                            .asBitmap()
         
     | 
| 
       66 
125 
     | 
    
         
             
                                            .load(photoURL)
         
     | 
| 
       67 
     | 
    
         
            -
                                            . 
     | 
| 
      
 126 
     | 
    
         
            +
                                            .into(object : CustomTarget<Bitmap>(){
         
     | 
| 
      
 127 
     | 
    
         
            +
                                                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
         
     | 
| 
       68 
     | 
    
         
            -
             
     | 
| 
      
 128 
     | 
    
         
            +
                                                    wm.setBitmap(resource)
         
     | 
| 
      
 129 
     | 
    
         
            +
                                                }
         
     | 
| 
      
 130 
     | 
    
         
            +
             
     | 
| 
       69 
     | 
    
         
            -
             
     | 
| 
      
 131 
     | 
    
         
            +
                                                override fun onLoadCleared(placeholder: Drawable?) {
         
     | 
| 
      
 132 
     | 
    
         
            +
             
     | 
| 
      
 133 
     | 
    
         
            +
                                                }
         
     | 
| 
      
 134 
     | 
    
         
            +
                                            })
         
     | 
| 
       70 
135 
     | 
    
         
             
                                    }catch(e:JSONException) {
         
     | 
| 
       71 
136 
     | 
    
         | 
| 
       72 
137 
     | 
    
         
             
                                    }
         
     | 
7
コードの修正
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -1,6 +1,6 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ### 前提・実現したいこと
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
            OkhttpでJSONを取得してそのからURLを抜き出して、
         
     | 
| 
      
 3 
     | 
    
         
            +
            [Okhttp](https://square.github.io/okhttp/)でJSONを取得してそのからURLを抜き出して、
         
     | 
| 
       4 
4 
     | 
    
         
             
            [Glide](https://bumptech.github.io/glide/doc/getting-started.html)(Android画像ダウンローダーライブラリ)を使って画像をBitMapで取得し、システム壁紙に設定したいです。
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
            ### 発生している問題・エラーメッセージ
         
     | 
6
前提条件の追加
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -1,5 +1,6 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ### 前提・実現したいこと
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
      
 3 
     | 
    
         
            +
            OkhttpでJSONを取得してそのからURLを抜き出して、
         
     | 
| 
       3 
4 
     | 
    
         
             
            [Glide](https://bumptech.github.io/glide/doc/getting-started.html)(Android画像ダウンローダーライブラリ)を使って画像をBitMapで取得し、システム壁紙に設定したいです。
         
     | 
| 
       4 
5 
     | 
    
         | 
| 
       5 
6 
     | 
    
         
             
            ### 発生している問題・エラーメッセージ
         
     | 
5
コードの編集
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -49,14 +49,29 @@ 
     | 
|
| 
       49 
49 
     | 
    
         
             
                    val setbtn = findViewById<Button>(R.id.setting) 
         
     | 
| 
       50 
50 
     | 
    
         
             
                    val wm = WallpaperManager.getInstance(this)
         
     | 
| 
       51 
51 
     | 
    
         
             
                    setbtn.setOnClickListener{ //ボタンが押されたら
         
     | 
| 
      
 52 
     | 
    
         
            +
                       val client = OkHttpClient()
         
     | 
| 
      
 53 
     | 
    
         
            +
                       val request = Request.Builder()
         
     | 
| 
      
 54 
     | 
    
         
            +
                            .url("JSONURL")//JSONのURLが入っているものとします
         
     | 
| 
      
 55 
     | 
    
         
            +
                            .build()
         
     | 
| 
      
 56 
     | 
    
         
            +
                        client.newCall(request).enqueue(object : Callback {
         
     | 
| 
      
 57 
     | 
    
         
            +
                            val mainHandler: Handler = Handler(Looper.getMainLooper())
         
     | 
| 
      
 58 
     | 
    
         
            +
                            override fun onResponse(call: Call, response: Response) {
         
     | 
| 
      
 59 
     | 
    
         
            +
                                val json = JSONObject(response.body!!.string())
         
     | 
| 
       52 
     | 
    
         
            -
             
     | 
| 
      
 60 
     | 
    
         
            +
                                mainHandler.post(Runnable(){
         
     | 
| 
      
 61 
     | 
    
         
            +
                                    try{
         
     | 
| 
      
 62 
     | 
    
         
            +
                                        val photoURL = json.getString("url")
         
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
      
 63 
     | 
    
         
            +
                                        val bitmap = Glide.with(this@MainActivity)
         
     | 
| 
       54 
64 
     | 
    
         
             
                                            .asBitmap()
         
     | 
| 
       55 
     | 
    
         
            -
                                            .load(photoURL) 
     | 
| 
      
 65 
     | 
    
         
            +
                                            .load(photoURL)
         
     | 
| 
       56 
66 
     | 
    
         
             
                                            .submit()
         
     | 
| 
       57 
     | 
    
         
            -
             
     | 
| 
      
 67 
     | 
    
         
            +
                                        wm.setBitmap(bitmap.get())
         
     | 
| 
       58 
     | 
    
         
            -
             
     | 
| 
      
 68 
     | 
    
         
            +
                                        Glide.with(this@MainActivity).clear(bitmap)
         
     | 
| 
      
 69 
     | 
    
         
            +
                                    }catch(e:JSONException) {
         
     | 
| 
      
 70 
     | 
    
         
            +
                                        
         
     | 
| 
      
 71 
     | 
    
         
            +
                                    }
         
     | 
| 
       59 
     | 
    
         
            -
             
     | 
| 
      
 72 
     | 
    
         
            +
                                })
         
     | 
| 
      
 73 
     | 
    
         
            +
                            }
         
     | 
| 
      
 74 
     | 
    
         
            +
                        })
         
     | 
| 
       60 
75 
     | 
    
         
             
                    }
         
     | 
| 
       61 
76 
     | 
    
         
             
            ```
         
     | 
| 
       62 
77 
     | 
    
         | 
4
修正
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -26,6 +26,21 @@ 
     | 
|
| 
       26 
26 
     | 
    
         
             
            ### 該当のソースコード
         
     | 
| 
       27 
27 
     | 
    
         | 
| 
       28 
28 
     | 
    
         
             
            ```Kotlin
         
     | 
| 
      
 29 
     | 
    
         
            +
            import android.app.WallpaperManager
         
     | 
| 
      
 30 
     | 
    
         
            +
            import android.content.Intent
         
     | 
| 
      
 31 
     | 
    
         
            +
            import android.net.Uri
         
     | 
| 
      
 32 
     | 
    
         
            +
            import android.os.Bundle
         
     | 
| 
      
 33 
     | 
    
         
            +
            import android.os.Handler
         
     | 
| 
      
 34 
     | 
    
         
            +
            import android.widget.Button
         
     | 
| 
      
 35 
     | 
    
         
            +
            import androidx.appcompat.app.AppCompatActivity
         
     | 
| 
      
 36 
     | 
    
         
            +
            import okhttp3.*
         
     | 
| 
      
 37 
     | 
    
         
            +
            import org.json.JSONObject
         
     | 
| 
      
 38 
     | 
    
         
            +
            import java.io.IOException
         
     | 
| 
      
 39 
     | 
    
         
            +
            import android.os.Looper
         
     | 
| 
      
 40 
     | 
    
         
            +
            import android.widget.ImageView
         
     | 
| 
      
 41 
     | 
    
         
            +
            import com.bumptech.glide.Glide
         
     | 
| 
      
 42 
     | 
    
         
            +
            import org.json.JSONException
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
       29 
44 
     | 
    
         
             
            class MainActivity : AppCompatActivity() {
         
     | 
| 
       30 
45 
     | 
    
         
             
                override fun onCreate(savedInstanceState: Bundle?) {
         
     | 
| 
       31 
46 
     | 
    
         
             
                    super.onCreate(savedInstanceState)
         
     | 
| 
         @@ -45,6 +60,11 @@ 
     | 
|
| 
       45 
60 
     | 
    
         
             
                    }
         
     | 
| 
       46 
61 
     | 
    
         
             
            ```
         
     | 
| 
       47 
62 
     | 
    
         | 
| 
      
 63 
     | 
    
         
            +
            AndroidManifest.xml(権限部分のみ)
         
     | 
| 
      
 64 
     | 
    
         
            +
            ```xml
         
     | 
| 
      
 65 
     | 
    
         
            +
            <uses-permission android:name="android.permission.SET_WALLPAPER" />
         
     | 
| 
      
 66 
     | 
    
         
            +
            <uses-permission android:name="android.permission.INTERNET" />
         
     | 
| 
      
 67 
     | 
    
         
            +
            ```
         
     | 
| 
       48 
68 
     | 
    
         
             
            ### 試したこと
         
     | 
| 
       49 
69 
     | 
    
         | 
| 
       50 
70 
     | 
    
         
             
            into()でImageViewに表示させると正常に表示されるのでおそらくGlideの使い方が間違ってます。
         
     | 
3
コードの修正
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -34,12 +34,15 @@ 
     | 
|
| 
       34 
34 
     | 
    
         
             
                    val setbtn = findViewById<Button>(R.id.setting) 
         
     | 
| 
       35 
35 
     | 
    
         
             
                    val wm = WallpaperManager.getInstance(this)
         
     | 
| 
       36 
36 
     | 
    
         
             
                    setbtn.setOnClickListener{ //ボタンが押されたら
         
     | 
| 
      
 37 
     | 
    
         
            +
                       mainHandler.post(Runnable(){
         
     | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
      
 38 
     | 
    
         
            +
                        val bitmap = Glide.with(this@MainActivity)
         
     | 
| 
       38 
39 
     | 
    
         
             
                                            .asBitmap()
         
     | 
| 
       39 
40 
     | 
    
         
             
                                            .load(photoURL) //photoURLは写真のURLが入っているものとします
         
     | 
| 
       40 
41 
     | 
    
         
             
                                            .submit()
         
     | 
| 
       41 
     | 
    
         
            -
             
     | 
| 
      
 42 
     | 
    
         
            +
                        wm.setBitmap(bitmap.get())
         
     | 
| 
       42 
     | 
    
         
            -
             
     | 
| 
      
 43 
     | 
    
         
            +
                        Glide.with(this@MainActivity).clear(bitmap)
         
     | 
| 
      
 44 
     | 
    
         
            +
                      })
         
     | 
| 
      
 45 
     | 
    
         
            +
                    }
         
     | 
| 
       43 
46 
     | 
    
         
             
            ```
         
     | 
| 
       44 
47 
     | 
    
         | 
| 
       45 
48 
     | 
    
         
             
            ### 試したこと
         
     | 
2
初心者アイコンの追加
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -59,4 +59,6 @@ 
     | 
|
| 
       59 
59 
     | 
    
         
             
            ```
         
     | 
| 
       60 
60 
     | 
    
         
             
            minSdk 25
         
     | 
| 
       61 
61 
     | 
    
         
             
            targetSdk 30
         
     | 
| 
       62 
     | 
    
         
            -
            ```
         
     | 
| 
      
 62 
     | 
    
         
            +
            ```
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
            Android Studio 2020.3.1
         
     | 
1
コードの追加
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -53,4 +53,10 @@ 
     | 
|
| 
       53 
53 
     | 
    
         
             
            implementation 'com.squareup.okhttp3:okhttp:4.9.0'
         
     | 
| 
       54 
54 
     | 
    
         
             
            implementation 'com.github.bumptech.glide:glide:4.11.0'
         
     | 
| 
       55 
55 
     | 
    
         
             
            annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
         
     | 
| 
      
 56 
     | 
    
         
            +
            ```
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
            ターゲットSDKと最小SDKです
         
     | 
| 
      
 59 
     | 
    
         
            +
            ```
         
     | 
| 
      
 60 
     | 
    
         
            +
            minSdk 25
         
     | 
| 
      
 61 
     | 
    
         
            +
            targetSdk 30
         
     | 
| 
       56 
62 
     | 
    
         
             
            ```
         
     |