์๋ฌ ๋ก๊ทธ ํ์ธ
Caused by: java.lang.IllegalStateException: You can't start or clear loads in RequestListener or Target callbacks. If you're trying to start a fallback request when a load fails, use RequestBuilder#error(RequestBuilder). Otherwise consider posting your into() or clear() calls to the main thread using a Handler instead.
๋ฌธ์ ์ ์ฝ๋
private void setData() {
...
Glide.with(imageView)
.asBitmap()
.load(profileImageUrl)
.into(new CustomTarget<Bitmap>(){
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
setBitmapToImage(resource)
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
...
}
protected void setBitmapToImage(Bitmap photo) {
...
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, strea);
Glide.with(imageView)
.load(stream.toByteArray())
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.circleCrop()
.transition(DrawableTransitionOptions.withCrossFade())
.into(imageView);
...
}
๋ก๊ทธ๋ฅผ ๋ณด๊ณ setBitmapToImage() ์์ ์๋ Glide ์ฝ๋์ error() ์ฝ๋๋ฅผ ์ฝ์ ํ๋ ๋ฑ์ ์๋๋ฅผ ํด๋ณด์์ผ๋ ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์ง ์์๋ค. ๐
ํด๊ฒฐ
https://stackoverflow.com/a/52610854
Android, Glide 4.3.0: If you must do so, consider posting your into() or clear() calls to the main thread using a Handler instea
Android Studio 3.2, Glide 4.3.0 I need to custom handle situation when image is not load (e.g. when url is not correct or server doesn't response). Here my code Fragment: public static void
stackoverflow.com
StackOverFlow์ ๋ต๋ณ์์ ํด๋น ์ด์์ ๊ดํ ๋ฌธ์ ๋งํฌ๋ฅผ ๊ณต์ ํด์ค์ ๋ฌธ์ ํด๊ฒฐ์ ์๊ฒ ๋๋ค.
Glide v4 : Debugging
Local Logs If you have access to the device, you can look for a few log lines using adb logcat or your IDE. You can enable logging for any tag mentioned here using: adb shell setprop log.tag. VERBOSE logs tend to be more verbose but contain more useful inf
bumptech.github.io
If you attempt to start a new load in onResourceReady or onLoadFailed in a Target or RequestListener, Glide will throw an exception. We throw this exception because itโs challenging for us to handle the load being recycled while itโs in the middle of notifying.
๋ฐ๋ฒ์ญ
๋ง์ฝ Target ์ด๋ RequestListener ์์ onResourceReady ํน์ onLoadFailed ์์ ์๋ก์ด ๋ก๋๋ฅผ ์์ํ๋ ๊ฑธ ์๋ํ๋ค๋ฉด, Glide๋ exception์ ๋ฐ์์ํฌ ๊ฒ๋๋ค. ์ฐ๋ฆฌ๋ ์ด exception์ ๋ฐ์์ํฌํ ๋ฐ์, ์๋ฆผ ์ค๊ฐ์ ์ฌํ์ฉ ์ค์ธ ๋ก๋๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ด๋ ต๊ธฐ ๋๋ฌธ์ด์ฃ .
the middle of notifying์ ์๋ฏธ๋ฅผ ์ ๋๋ก ์ดํดํ์ง ๋ชปํ๋ค. ๋ฌด์์ notifyํ๋ค๋ ๊ฒ์ธ์ง?๐ ํ๋์ ๋ฆฌ์์ค๋ฅผ ๋๊ณ ๋ ๊ฐ์ Glide๋ก load๋ฅผ ํ๊ฒ๋๋ค๋ฉด, Glide ๋ด๋ถ์์ ๊ผฌ์ผ ์ ์๋ ํ์์ด ์๋๋ณด๋ค, ๊ทธ๊ฒ load recycle๊ณผ ๊ด๋ จ์๋ ๋ณด๋ค, ์ ๋ ์ถ๋ก ํ๊ณ ์๋ค.
setData() ์์ Glide ์ฝ๋๋ฅผ ๋ง์ ธ์ผ ํ๋ ๊ฒ์ด์๋ค. setData() ์ Glide์ CustomTarget์ onResourceReady() ์ฝ๋ฐฑ์์ ํธ์ถํ๋ setBitmapToImage()์ด ๋ค์ Glide๋ฅผ ํธ์ถํด์ ํด๋น ์ด์๊ฐ ๋ฐ์ํ๋ ๊ฒ์ด์๋ค.
๋ฌธ์์์๋ onLoadFailed() ์ฝ๋ฐฑ ์์ Glide๋ฅผ ํธ์ถํ๋ ๊ฒ ๋์ ์ RequestBuilder์ error()๋ฅผ ์ฌ์ฉํ๋ผ๊ณ ํ๋๋ฐ, ๋๋ onResourceReady()์์ ํธ์ถํ๋ ๊ฑฐ๋ผ ๊ทธ๋ ๊ฒ ํ ์๋ ์์๋ค. 4.3.0 ๋ฒ์ ์ด์ ์ ํด๊ฒฐ ๋ฐฉ์์ ๊ฐ์ ธ์์ ์ฝ๋๋ฅผ ์์ ํ๊ณ , ์ ์๋์์ ํ์ธํ๋ค.
์ฝ๋ ์์
private void setData() {
...
Glide.with(imageView)
.asBitmap()
.load(profileImageUrl)
.into(new CustomTarget<Bitmap>(){
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
new Handler().post(() -> setBitmapToImage(resource));
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
...
}