質問編集履歴
12
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
[Okhttp](https://square.github.io/okhttp/)でJSONを取得してそこからURLを抜き出して、
|
6
|
-
|
7
5
|
[Glide](https://bumptech.github.io/glide/doc/getting-started.html)(画像ダウンローダーライブラリ)を使って画像をbitmapで取得し、システム壁紙に設定したいです。
|
8
6
|
|
9
7
|
|
@@ -134,74 +132,6 @@
|
|
134
132
|
|
135
133
|
```Kotlin
|
136
134
|
|
137
|
-
import android.app.WallpaperManager
|
138
|
-
|
139
|
-
import android.content.Intent
|
140
|
-
|
141
|
-
import android.net.Uri
|
142
|
-
|
143
|
-
import android.os.Bundle
|
144
|
-
|
145
|
-
import android.os.Handler
|
146
|
-
|
147
|
-
import android.widget.Button
|
148
|
-
|
149
|
-
import androidx.appcompat.app.AppCompatActivity
|
150
|
-
|
151
|
-
import okhttp3.*
|
152
|
-
|
153
|
-
import org.json.JSONObject
|
154
|
-
|
155
|
-
import java.io.IOException
|
156
|
-
|
157
|
-
import android.os.Looper
|
158
|
-
|
159
|
-
import android.widget.ImageView
|
160
|
-
|
161
|
-
import com.bumptech.glide.Glide
|
162
|
-
|
163
|
-
import org.json.JSONException
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
class MainActivity : AppCompatActivity() {
|
168
|
-
|
169
|
-
override fun onCreate(savedInstanceState: Bundle?) {
|
170
|
-
|
171
|
-
super.onCreate(savedInstanceState)
|
172
|
-
|
173
|
-
setContentView(R.layout.activity_main)
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
val setbtn = findViewById<Button>(R.id.setting)
|
178
|
-
|
179
|
-
val wm = WallpaperManager.getInstance(this)
|
180
|
-
|
181
|
-
setbtn.setOnClickListener{ //ボタンが押されたら
|
182
|
-
|
183
|
-
val client = OkHttpClient()
|
184
|
-
|
185
|
-
val request = Request.Builder()
|
186
|
-
|
187
|
-
.url("JSONURL")//JSONのURLが入っているものとします
|
188
|
-
|
189
|
-
.build()
|
190
|
-
|
191
|
-
client.newCall(request).enqueue(object : Callback {
|
192
|
-
|
193
|
-
val mainHandler: Handler = Handler(Looper.getMainLooper())
|
194
|
-
|
195
|
-
override fun onResponse(call: Call, response: Response) {
|
196
|
-
|
197
|
-
val json = JSONObject(response.body!!.string())
|
198
|
-
|
199
|
-
mainHandler.post(Runnable(){
|
200
|
-
|
201
|
-
try{
|
202
|
-
|
203
|
-
val photoURL = json.getString("url")
|
204
|
-
|
205
135
|
Glide.with(this@MainActivity)
|
206
136
|
|
207
137
|
.asBitmap()
|
@@ -258,9 +188,7 @@
|
|
258
188
|
|
259
189
|
|
260
190
|
|
261
|
-
|
191
|
+
into()でImageViewに表示させると正常に表示されるのでおそらくGlideの使い方が間違ってます。
|
262
|
-
|
263
|
-
JSONの取得が何故か出来なくなりました。JSONにはブラウザでアクセスできるので、URLが誤りや切れているなどはあり得ないと思います。
|
264
192
|
|
265
193
|
|
266
194
|
|
11
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,19 +16,53 @@
|
|
16
16
|
|
17
17
|
E/AndroidRuntime: FATAL EXCEPTION: main
|
18
18
|
|
19
|
-
Process: com.example.wallpaper, PID:
|
19
|
+
Process: com.example.wallpaper, PID: 7803
|
20
|
+
|
20
|
-
|
21
|
+
android.os.NetworkOnMainThreadException
|
22
|
+
|
21
|
-
|
23
|
+
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
|
22
|
-
|
24
|
+
|
23
|
-
at com.
|
25
|
+
at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:424)
|
26
|
+
|
24
|
-
|
27
|
+
at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:564)
|
28
|
+
|
29
|
+
at okio.InputStreamSource.read(JvmOkio.kt:90)
|
30
|
+
|
31
|
+
at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:129)
|
32
|
+
|
25
|
-
at
|
33
|
+
at okio.RealBufferedSource.request(RealBufferedSource.kt:206)
|
34
|
+
|
26
|
-
|
35
|
+
at okio.RealBufferedSource.require(RealBufferedSource.kt:199)
|
36
|
+
|
27
|
-
at
|
37
|
+
at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.kt:381)
|
38
|
+
|
28
|
-
|
39
|
+
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:429)
|
40
|
+
|
41
|
+
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408)
|
42
|
+
|
43
|
+
at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:276)
|
44
|
+
|
45
|
+
at okio.RealBufferedSource.read(RealBufferedSource.kt:189)
|
46
|
+
|
47
|
+
at okio.RealBufferedSource.exhausted(RealBufferedSource.kt:197)
|
48
|
+
|
49
|
+
at okio.InflaterSource.refill(InflaterSource.kt:112)
|
50
|
+
|
51
|
+
at okio.InflaterSource.readOrInflate(InflaterSource.kt:76)
|
52
|
+
|
53
|
+
at okio.InflaterSource.read(InflaterSource.kt:49)
|
54
|
+
|
55
|
+
at okio.GzipSource.read(GzipSource.kt:69)
|
56
|
+
|
57
|
+
at okio.Buffer.writeAll(Buffer.kt:1642)
|
58
|
+
|
59
|
+
at okio.RealBufferedSource.readString(RealBufferedSource.kt:95)
|
60
|
+
|
61
|
+
at okhttp3.ResponseBody.string(ResponseBody.kt:187)
|
62
|
+
|
29
|
-
at com.example.wallpaper.MainActivity$onCreate$2$1.onResponse$lambda-1(MainActivity.kt:5
|
63
|
+
at com.example.wallpaper.MainActivity$onCreate$2$1.onResponse$lambda-1(MainActivity.kt:55)
|
30
|
-
|
64
|
+
|
31
|
-
at com.example.wallpaper.MainActivity$onCreate$2$1.$r8$lambda$
|
65
|
+
at com.example.wallpaper.MainActivity$onCreate$2$1.$r8$lambda$L-f6eyl8kI29t2zf84oLw39ndx8(Unknown Source:0)
|
32
66
|
|
33
67
|
at com.example.wallpaper.MainActivity$onCreate$2$1$$ExternalSyntheticLambda1.run(Unknown Source:6)
|
34
68
|
|
@@ -46,19 +80,7 @@
|
|
46
80
|
|
47
81
|
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
|
48
82
|
|
49
|
-
```
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
修正後エラー
|
54
|
-
|
55
|
-
```
|
56
|
-
|
57
|
-
E/AndroidRuntime: FATAL EXCEPTION: main
|
58
|
-
|
59
|
-
Process: com.example.wallpaper, PID: 7803
|
60
|
-
|
61
|
-
android.os.NetworkOnMainThreadException
|
83
|
+
Suppressed: android.os.NetworkOnMainThreadException
|
62
84
|
|
63
85
|
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
|
64
86
|
|
@@ -70,78 +92,16 @@
|
|
70
92
|
|
71
93
|
at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:129)
|
72
94
|
|
73
|
-
at okio.RealBufferedSource.
|
95
|
+
at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:427)
|
96
|
+
|
74
|
-
|
97
|
+
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:320)
|
98
|
+
|
75
|
-
at okio.RealBufferedSource.re
|
99
|
+
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:105)
|
76
|
-
|
77
|
-
|
100
|
+
|
78
|
-
|
79
|
-
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:42
|
101
|
+
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:426)
|
80
102
|
|
81
103
|
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408)
|
82
104
|
|
83
|
-
at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:276)
|
84
|
-
|
85
|
-
at okio.RealBufferedSource.read(RealBufferedSource.kt:189)
|
86
|
-
|
87
|
-
at okio.RealBufferedSource.exhausted(RealBufferedSource.kt:197)
|
88
|
-
|
89
|
-
at okio.InflaterSource.refill(InflaterSource.kt:112)
|
90
|
-
|
91
|
-
at okio.InflaterSource.readOrInflate(InflaterSource.kt:76)
|
92
|
-
|
93
|
-
at okio.InflaterSource.read(InflaterSource.kt:49)
|
94
|
-
|
95
|
-
at okio.GzipSource.read(GzipSource.kt:69)
|
96
|
-
|
97
|
-
at okio.Buffer.writeAll(Buffer.kt:1642)
|
98
|
-
|
99
|
-
at okio.RealBufferedSource.readString(RealBufferedSource.kt:95)
|
100
|
-
|
101
|
-
at okhttp3.ResponseBody.string(ResponseBody.kt:187)
|
102
|
-
|
103
|
-
at com.example.wallpaper.MainActivity$onCreate$2$1.onResponse$lambda-1(MainActivity.kt:55)
|
104
|
-
|
105
|
-
at com.example.wallpaper.MainActivity$onCreate$2$1.$r8$lambda$L-f6eyl8kI29t2zf84oLw39ndx8(Unknown Source:0)
|
106
|
-
|
107
|
-
at com.example.wallpaper.MainActivity$onCreate$2$1$$ExternalSyntheticLambda1.run(Unknown Source:6)
|
108
|
-
|
109
|
-
at android.os.Handler.handleCallback(Handler.java:883)
|
110
|
-
|
111
|
-
at android.os.Handler.dispatchMessage(Handler.java:100)
|
112
|
-
|
113
|
-
at android.os.Looper.loop(Looper.java:237)
|
114
|
-
|
115
|
-
at android.app.ActivityThread.main(ActivityThread.java:8125)
|
116
|
-
|
117
|
-
at java.lang.reflect.Method.invoke(Native Method)
|
118
|
-
|
119
|
-
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
|
120
|
-
|
121
|
-
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
|
122
|
-
|
123
|
-
Suppressed: android.os.NetworkOnMainThreadException
|
124
|
-
|
125
|
-
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
|
126
|
-
|
127
|
-
at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:424)
|
128
|
-
|
129
|
-
at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:564)
|
130
|
-
|
131
|
-
at okio.InputStreamSource.read(JvmOkio.kt:90)
|
132
|
-
|
133
|
-
at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:129)
|
134
|
-
|
135
|
-
at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:427)
|
136
|
-
|
137
|
-
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:320)
|
138
|
-
|
139
|
-
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:105)
|
140
|
-
|
141
|
-
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:426)
|
142
|
-
|
143
|
-
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408)
|
144
|
-
|
145
105
|
at okhttp3.internal.Util.skipAll(Util.kt:337)
|
146
106
|
|
147
107
|
at okhttp3.internal.Util.discard(Util.kt:358)
|
10
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
[Okhttp](https://square.github.io/okhttp/)でJSONを取得してそ
|
5
|
+
[Okhttp](https://square.github.io/okhttp/)でJSONを取得してそこからURLを抜き出して、
|
6
|
-
|
6
|
+
|
7
|
-
[Glide](https://bumptech.github.io/glide/doc/getting-started.html)(
|
7
|
+
[Glide](https://bumptech.github.io/glide/doc/getting-started.html)(画像ダウンローダーライブラリ)を使って画像をbitmapで取得し、システム壁紙に設定したいです。
|
8
8
|
|
9
9
|
|
10
10
|
|
@@ -298,7 +298,9 @@
|
|
298
298
|
|
299
299
|
|
300
300
|
|
301
|
-
into()でImageViewに表示させると正常に表示されるのでおそらくGlideの使い方が間違ってます。
|
301
|
+
~~into()でImageViewに表示させると正常に表示されるのでおそらくGlideの使い方が間違ってます。~~
|
302
|
+
|
303
|
+
JSONの取得が何故か出来なくなりました。JSONにはブラウザでアクセスできるので、URLが誤りや切れているなどはあり得ないと思います。
|
302
304
|
|
303
305
|
|
304
306
|
|
9
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -242,7 +242,7 @@
|
|
242
242
|
|
243
243
|
val photoURL = json.getString("url")
|
244
244
|
|
245
|
-
|
245
|
+
Glide.with(this@MainActivity)
|
246
246
|
|
247
247
|
.asBitmap()
|
248
248
|
|
8
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -50,6 +50,124 @@
|
|
50
50
|
|
51
51
|
|
52
52
|
|
53
|
+
修正後エラー
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
E/AndroidRuntime: FATAL EXCEPTION: main
|
58
|
+
|
59
|
+
Process: com.example.wallpaper, PID: 7803
|
60
|
+
|
61
|
+
android.os.NetworkOnMainThreadException
|
62
|
+
|
63
|
+
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
|
64
|
+
|
65
|
+
at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:424)
|
66
|
+
|
67
|
+
at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:564)
|
68
|
+
|
69
|
+
at okio.InputStreamSource.read(JvmOkio.kt:90)
|
70
|
+
|
71
|
+
at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:129)
|
72
|
+
|
73
|
+
at okio.RealBufferedSource.request(RealBufferedSource.kt:206)
|
74
|
+
|
75
|
+
at okio.RealBufferedSource.require(RealBufferedSource.kt:199)
|
76
|
+
|
77
|
+
at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.kt:381)
|
78
|
+
|
79
|
+
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:429)
|
80
|
+
|
81
|
+
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408)
|
82
|
+
|
83
|
+
at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:276)
|
84
|
+
|
85
|
+
at okio.RealBufferedSource.read(RealBufferedSource.kt:189)
|
86
|
+
|
87
|
+
at okio.RealBufferedSource.exhausted(RealBufferedSource.kt:197)
|
88
|
+
|
89
|
+
at okio.InflaterSource.refill(InflaterSource.kt:112)
|
90
|
+
|
91
|
+
at okio.InflaterSource.readOrInflate(InflaterSource.kt:76)
|
92
|
+
|
93
|
+
at okio.InflaterSource.read(InflaterSource.kt:49)
|
94
|
+
|
95
|
+
at okio.GzipSource.read(GzipSource.kt:69)
|
96
|
+
|
97
|
+
at okio.Buffer.writeAll(Buffer.kt:1642)
|
98
|
+
|
99
|
+
at okio.RealBufferedSource.readString(RealBufferedSource.kt:95)
|
100
|
+
|
101
|
+
at okhttp3.ResponseBody.string(ResponseBody.kt:187)
|
102
|
+
|
103
|
+
at com.example.wallpaper.MainActivity$onCreate$2$1.onResponse$lambda-1(MainActivity.kt:55)
|
104
|
+
|
105
|
+
at com.example.wallpaper.MainActivity$onCreate$2$1.$r8$lambda$L-f6eyl8kI29t2zf84oLw39ndx8(Unknown Source:0)
|
106
|
+
|
107
|
+
at com.example.wallpaper.MainActivity$onCreate$2$1$$ExternalSyntheticLambda1.run(Unknown Source:6)
|
108
|
+
|
109
|
+
at android.os.Handler.handleCallback(Handler.java:883)
|
110
|
+
|
111
|
+
at android.os.Handler.dispatchMessage(Handler.java:100)
|
112
|
+
|
113
|
+
at android.os.Looper.loop(Looper.java:237)
|
114
|
+
|
115
|
+
at android.app.ActivityThread.main(ActivityThread.java:8125)
|
116
|
+
|
117
|
+
at java.lang.reflect.Method.invoke(Native Method)
|
118
|
+
|
119
|
+
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
|
120
|
+
|
121
|
+
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
|
122
|
+
|
123
|
+
Suppressed: android.os.NetworkOnMainThreadException
|
124
|
+
|
125
|
+
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
|
126
|
+
|
127
|
+
at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:424)
|
128
|
+
|
129
|
+
at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:564)
|
130
|
+
|
131
|
+
at okio.InputStreamSource.read(JvmOkio.kt:90)
|
132
|
+
|
133
|
+
at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:129)
|
134
|
+
|
135
|
+
at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:427)
|
136
|
+
|
137
|
+
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:320)
|
138
|
+
|
139
|
+
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:105)
|
140
|
+
|
141
|
+
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:426)
|
142
|
+
|
143
|
+
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408)
|
144
|
+
|
145
|
+
at okhttp3.internal.Util.skipAll(Util.kt:337)
|
146
|
+
|
147
|
+
at okhttp3.internal.Util.discard(Util.kt:358)
|
148
|
+
|
149
|
+
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.close(Http1ExchangeCodec.kt:450)
|
150
|
+
|
151
|
+
at okio.ForwardingSource.close(ForwardingSource.kt:34)
|
152
|
+
|
153
|
+
at okhttp3.internal.connection.Exchange$ResponseBodySource.close(Exchange.kt:309)
|
154
|
+
|
155
|
+
at okio.RealBufferedSource.close(RealBufferedSource.kt:477)
|
156
|
+
|
157
|
+
at okio.RealBufferedSource.close(RealBufferedSource.kt:477)
|
158
|
+
|
159
|
+
at okio.InflaterSource.close(InflaterSource.kt:136)
|
160
|
+
|
161
|
+
at okio.GzipSource.close(GzipSource.kt:171)
|
162
|
+
|
163
|
+
at okio.RealBufferedSource.close(RealBufferedSource.kt:477)
|
164
|
+
|
165
|
+
at kotlin.io.CloseableKt.closeFinally(Closeable.kt:60)
|
166
|
+
|
167
|
+
at okhttp3.ResponseBody.string(ResponseBody.kt:186)
|
168
|
+
|
169
|
+
```
|
170
|
+
|
53
171
|
### 該当のソースコード
|
54
172
|
|
55
173
|
|
@@ -130,11 +248,23 @@
|
|
130
248
|
|
131
249
|
.load(photoURL)
|
132
250
|
|
133
|
-
.s
|
251
|
+
.into(object : CustomTarget<Bitmap>(){
|
252
|
+
|
134
|
-
|
253
|
+
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
|
254
|
+
|
135
|
-
wm.setBitmap(
|
255
|
+
wm.setBitmap(resource)
|
256
|
+
|
136
|
-
|
257
|
+
}
|
258
|
+
|
259
|
+
|
260
|
+
|
137
|
-
|
261
|
+
override fun onLoadCleared(placeholder: Drawable?) {
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
})
|
138
268
|
|
139
269
|
}catch(e:JSONException) {
|
140
270
|
|
7
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
OkhttpでJSONを取得してそのからURLを抜き出して、
|
5
|
+
[Okhttp](https://square.github.io/okhttp/)でJSONを取得してそのからURLを抜き出して、
|
6
6
|
|
7
7
|
[Glide](https://bumptech.github.io/glide/doc/getting-started.html)(Android画像ダウンローダーライブラリ)を使って画像をBitMapで取得し、システム壁紙に設定したいです。
|
8
8
|
|
6
前提条件の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
OkhttpでJSONを取得してそのからURLを抜き出して、
|
6
|
+
|
5
7
|
[Glide](https://bumptech.github.io/glide/doc/getting-started.html)(Android画像ダウンローダーライブラリ)を使って画像をBitMapで取得し、システム壁紙に設定したいです。
|
6
8
|
|
7
9
|
|
5
コードの編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -100,21 +100,51 @@
|
|
100
100
|
|
101
101
|
setbtn.setOnClickListener{ //ボタンが押されたら
|
102
102
|
|
103
|
+
val client = OkHttpClient()
|
104
|
+
|
105
|
+
val request = Request.Builder()
|
106
|
+
|
107
|
+
.url("JSONURL")//JSONのURLが入っているものとします
|
108
|
+
|
109
|
+
.build()
|
110
|
+
|
111
|
+
client.newCall(request).enqueue(object : Callback {
|
112
|
+
|
113
|
+
val mainHandler: Handler = Handler(Looper.getMainLooper())
|
114
|
+
|
115
|
+
override fun onResponse(call: Call, response: Response) {
|
116
|
+
|
117
|
+
val json = JSONObject(response.body!!.string())
|
118
|
+
|
103
|
-
mainHandler.post(Runnable(){
|
119
|
+
mainHandler.post(Runnable(){
|
120
|
+
|
104
|
-
|
121
|
+
try{
|
122
|
+
|
123
|
+
val photoURL = json.getString("url")
|
124
|
+
|
105
|
-
val bitmap = Glide.with(this@MainActivity)
|
125
|
+
val bitmap = Glide.with(this@MainActivity)
|
106
126
|
|
107
127
|
.asBitmap()
|
108
128
|
|
109
|
-
.load(photoURL)
|
129
|
+
.load(photoURL)
|
110
130
|
|
111
131
|
.submit()
|
112
132
|
|
113
|
-
wm.setBitmap(bitmap.get())
|
133
|
+
wm.setBitmap(bitmap.get())
|
114
|
-
|
134
|
+
|
115
|
-
Glide.with(this@MainActivity).clear(bitmap)
|
135
|
+
Glide.with(this@MainActivity).clear(bitmap)
|
136
|
+
|
116
|
-
|
137
|
+
}catch(e:JSONException) {
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
}
|
142
|
+
|
117
|
-
})
|
143
|
+
})
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
})
|
118
148
|
|
119
149
|
}
|
120
150
|
|
4
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -54,6 +54,36 @@
|
|
54
54
|
|
55
55
|
```Kotlin
|
56
56
|
|
57
|
+
import android.app.WallpaperManager
|
58
|
+
|
59
|
+
import android.content.Intent
|
60
|
+
|
61
|
+
import android.net.Uri
|
62
|
+
|
63
|
+
import android.os.Bundle
|
64
|
+
|
65
|
+
import android.os.Handler
|
66
|
+
|
67
|
+
import android.widget.Button
|
68
|
+
|
69
|
+
import androidx.appcompat.app.AppCompatActivity
|
70
|
+
|
71
|
+
import okhttp3.*
|
72
|
+
|
73
|
+
import org.json.JSONObject
|
74
|
+
|
75
|
+
import java.io.IOException
|
76
|
+
|
77
|
+
import android.os.Looper
|
78
|
+
|
79
|
+
import android.widget.ImageView
|
80
|
+
|
81
|
+
import com.bumptech.glide.Glide
|
82
|
+
|
83
|
+
import org.json.JSONException
|
84
|
+
|
85
|
+
|
86
|
+
|
57
87
|
class MainActivity : AppCompatActivity() {
|
58
88
|
|
59
89
|
override fun onCreate(savedInstanceState: Bundle?) {
|
@@ -91,6 +121,16 @@
|
|
91
121
|
```
|
92
122
|
|
93
123
|
|
124
|
+
|
125
|
+
AndroidManifest.xml(権限部分のみ)
|
126
|
+
|
127
|
+
```xml
|
128
|
+
|
129
|
+
<uses-permission android:name="android.permission.SET_WALLPAPER" />
|
130
|
+
|
131
|
+
<uses-permission android:name="android.permission.INTERNET" />
|
132
|
+
|
133
|
+
```
|
94
134
|
|
95
135
|
### 試したこと
|
96
136
|
|
3
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -70,7 +70,9 @@
|
|
70
70
|
|
71
71
|
setbtn.setOnClickListener{ //ボタンが押されたら
|
72
72
|
|
73
|
+
mainHandler.post(Runnable(){
|
74
|
+
|
73
|
-
val bitmap = Glide.with(this@MainActivity)
|
75
|
+
val bitmap = Glide.with(this@MainActivity)
|
74
76
|
|
75
77
|
.asBitmap()
|
76
78
|
|
@@ -78,9 +80,13 @@
|
|
78
80
|
|
79
81
|
.submit()
|
80
82
|
|
81
|
-
wm.setBitmap(bitmap.get())
|
83
|
+
wm.setBitmap(bitmap.get())
|
82
84
|
|
83
|
-
Glide.with(this@MainActivity).clear(bitmap)
|
85
|
+
Glide.with(this@MainActivity).clear(bitmap)
|
86
|
+
|
87
|
+
})
|
88
|
+
|
89
|
+
}
|
84
90
|
|
85
91
|
```
|
86
92
|
|
2
初心者アイコンの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -121,3 +121,7 @@
|
|
121
121
|
targetSdk 30
|
122
122
|
|
123
123
|
```
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
Android Studio 2020.3.1
|
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -109,3 +109,15 @@
|
|
109
109
|
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
|
110
110
|
|
111
111
|
```
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
ターゲットSDKと最小SDKです
|
116
|
+
|
117
|
+
```
|
118
|
+
|
119
|
+
minSdk 25
|
120
|
+
|
121
|
+
targetSdk 30
|
122
|
+
|
123
|
+
```
|