26

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.

Sam Judd
  • 7,317
  • 1
  • 38
  • 38
Ukubu
  • 810
  • 1
  • 6
  • 18
  • 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 Answers2

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
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