I know it isn't very practical to load bitmaps from the device storage synchronously, but I really have to do it. I haven't figured out any way to do this.
Asked
Active
Viewed 1.4k times
26
-
I figure they could not see a usecase where that would be necessary. – EpicPandaForce Feb 09 '16 at 16:10
-
*but I really have to do it.* would you mind elaborating a little bit on this point ? – Blackbelt Feb 09 '16 at 16:11
-
I use a library and one of its method is responsible for managing a notification. There's a function I have to implement and it returns a notification for the library. I use a RemoteView and it contains an ImageView, but because I can not modify the notification after it has been created, only if I recreate it, I have to "delay" the creation of the notification after the bitmap will have been loaded. – Ukubu Feb 09 '16 at 16:30
-
Please be more specific about what you're trying to do. "From device storage" is vague. What format do you need it to take in memory upon completion? – Doug Stevenson Feb 09 '16 at 18:04
-
I pass the uri of album arts (think about audio files) to Glide. – Ukubu Feb 09 '16 at 20:41
-
If you are doing work in WorkManager you need to make the code flow sync, but you could still also use a CountDownLatch. – Ezequiel Adrian May 26 '23 at 02:56
2 Answers
50
Yes is possible and is in glide documentation.
For example if you need to retrive the Bitmap synchronously you can do:
Glide V3:
Bitmap myBitmap = Glide.with(applicationContext)
.load(yourUrl)
.asBitmap()
.into(500, 500)
.get()
Glide v4:
FutureTarget<Bitmap> futureBitmap = Glide.with(applicationContext)
.asBitmap()
.load(yourURL))
.submit();
Bitmap myBitmap = futureBitmap.get();
Note: This code need to be run in the background or the app will crash.

Yayo Arellano
- 3,575
- 23
- 28
-
1If you do this on main thread it will throw an error saying you have to call this on another thread. Blocking the thread sure, but make sure its not the main thread. – Neon Warge Mar 20 '17 at 03:26
-
0
Using interfaces to load data after image is loaded is a better option.
Create a new file called OnglideLoaded.java file like this:
import android.graphics.drawable.Drawable;
public interface OnGlideLoaded {
void onGlideLoaded(Drawable drawable);
}
Now create setter method for it like this where glide is:
public void setOnGlideLoaded(OnGlideLoaded onGlideLoaded) {
this.onGlideLoaded = onGlideLoaded;
}
Not call it inside Glide Target:
Glide.with(context)
.asBitmap()
.load(uri)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
icon = resource;
if (onGlideLoaded!=null){
//This will send the image to every registered interface
onGlideLoaded.onGlideLoaded(icon);
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
Now call the interface like this where you want the image:
reference.setOnGlideLoaded(new OnGlideLoaded() {
@Override
public void onGlideLoaded(Drawable drawable) {
imageView.setImageBitmap(drawable);
}
});
Here, reference will be the object where you created the setter method.

RHS.Dev
- 412
- 6
- 18