-1

I try to use Glide to load an external image and display it in a toolbar. I have the following code (inside a Fragment):

   ImageView image = null;

    Glide.with(this)
            .load("https://images.unsplash.com/photo-1612138210955-363d1ba3477d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max")
            .into(image);
    binding.toolbar.setBackgroundResource(image);

The method 'binding.toolbar.setBackgroundResource()' requires an int but the loaded image from Glide is an imageView. Now my question is, how can I convert the loaded image from Glide into an int sucht that I can use the method 'binding.toolbar.setBackgroundResource(image)'? I'd appreciate every comment.

Update: I tried the suggested code from one answer which looks like this:

 ImageView image = new ImageView(getContext());
    image.setDrawingCacheEnabled(true);
    Glide.with(this).load("https://images.unsplash.com/photo-1614907301762-405163ba662f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80")
            .into(image);
    Bitmap bmap = image.getDrawingCache();
    binding.toolbar.setBackground(new BitmapDrawable(getResources(), bmap));

Basically I do not get an error now. HOWEVER, the code should set the background of a toolbar. Basically the toolbar looks quite bad when using the suggested code because I can't see the picture but just a white area.I think the problem is the URL. I tried it with a non-existing URL and I get the same result. Just a white area. So this means that Glide can't load the image altough the URL itself is correct. Can you think about a specific reason why this problem occurs? –

As I have still not received an answer containing a solution to my problem, I would like to ask you again whether you know how to solve the problem? How can I just load an Image via Glide from a URL and insert it as the background of a toolbar?

VanessaF
  • 515
  • 11
  • 36
  • Does this answer your question? [Set Background Image to Relative Layout using Glide in Android](https://stackoverflow.com/questions/33971626/set-background-image-to-relative-layout-using-glide-in-android) – ADM Feb 28 '21 at 10:19
  • Why can't your use setBackgroundDrawable() or just setBackground(), is it not there for toolbar ? It will be easier to make drawable from bitmap. – Android Killer Feb 28 '21 at 10:23
  • Thanks AndroidKiller for your comment. When using setBackground() I still get an error telling me Required type: Drawable Provided: ImageView – VanessaF Feb 28 '21 at 10:33
  • @AndroidKiller: Any comments on my last comments? I'd highly appreciate every further comment from you – VanessaF Mar 01 '21 at 16:31
  • @VanessaF i have posted the solution as answer, can you please try that ??? – Android Killer Mar 01 '21 at 16:50

2 Answers2

0

Please try this approach:

    image.setDrawingCacheEnabled(true);
    Glide.with(this).load("https://images.unsplash.com/photo-1612138210955-363d1ba3477d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max")
            .into(image);
    Bitmap bmap = image.getDrawingCache();
    binding.toolbar.setBackground(new BitmapDrawable(getResources(), bmap));

Try this approach please :

Kotlin:

                Glide.with(image)
                .asBitmap()
                .load("https://images.unsplash.com/photo-1612138210955-363d1ba3477d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max")
                .into(object : BitmapImageViewTarget(image) {
                    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                        binding.toolbar.setBackground(resource);
                    }

                })

Java:

Glide.with(image)
                .asBitmap()
                .load("https://images.unsplash.com/photo-1612138210955-363d1ba3477d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max")
                .into(new RequestListener<Drawable>() {
        @Override
        public void onLoadFailed(Exception e, Object model, Target<Drawable> target, boolean isFirstResource) {
        }

        @Override
        public void onResourceReady(Drawable resource, Object model, Target<Drawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            binding.toolbar.setBackground(resource);
        }
    })
Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • Thanks for your answer. When I use your suggested code I get a null pointer exception "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setDrawingCacheEnabled(boolean)' on a null object reference". I still have the line " ImageView image = null;" in my code which might be the reason for the null pointer. However, I do not know how to initialize an emptry ImageView? – VanessaF Mar 02 '21 at 18:33
  • ImageView image = new ImageView(this) – Android Killer Mar 03 '21 at 15:35
  • Thanks for your answer and effort Android Killer. I had to change your code to "ImageView image = new ImageView(getContext());" otherwise I could not compile it. Basically I do not get an error now. HOWEVER, the code should set the background of a toolbar. Basically the fragment looks quite bad when using your suggested code because I can't see the picture but just a white area. – VanessaF Mar 03 '21 at 18:41
  • I also tried it with another picture source but the problem remains. I can only see a white area – VanessaF Mar 03 '21 at 18:51
  • Thanks for your answers. Any comments on my last comments? I'd highly appreciate any further comments from you – VanessaF Mar 04 '21 at 18:24
  • Any further comments about my problem? Can you imagine why this problem occurs? – VanessaF Mar 05 '21 at 18:20
  • I think the problem is the URL. I tried it with a non-existing URL and I get the same result. Just a white area. So this means that Glide can't load the image altough the URL itself is correct. Can you think about a specific reason why this problem occurs? – VanessaF Mar 06 '21 at 09:13
  • ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(this, R.color.green));. define green color code in your colors.xml and use and see the result. If it works then problem with glide else no. – Android Killer Mar 07 '21 at 18:06
  • Thanks Android Killer for your answer. I tried what you said and it works. I can see a green background. So this means that there is a problem with Glide or with your suggested code. Do you have any further ideas what the reason might be? Do you use Glide yourself and do you also use exactly your suggested code? – VanessaF Mar 10 '21 at 18:01
  • @VanessaF Updated the answer, try the second approach please. – Android Killer Mar 11 '21 at 06:26
  • Thanks Android Killer for your answer. When I use your suggested code I get a lot of error messages. Is this really Java code (and not maybe Kotlin)? For me also the notation is quite strange – VanessaF Mar 12 '21 at 18:02
  • Thanks for your answer and effort. Any comment to my last comment? – VanessaF Mar 14 '21 at 08:16
  • @VanessaF i have updated with java code, please try. and try to search over the internet, yuo will find a lot of examples. – Android Killer Mar 14 '21 at 09:32
  • Thanks Android Killer for your answer and effort. I really appreicate it. When using your suggested code I get the error message "Class 'Anonymous class derived from RequestListener' must either be declared abstract or implement abstract method 'onLoadFailed(GlideException, Object, Target, boolean)' in 'RequestListener'". I also tried to solve the problem but I could not. – VanessaF Mar 14 '21 at 10:05
  • You can be sure that I checked the Internet for Glide code. After several hours of trying without success as I always get an error message I decided to ask this question in Stackoverflow. You can see in my original post and in the comments that I did try a lot. Unfortunately so far it was not successfull. Either I get an error message or I just see a white background – VanessaF Mar 14 '21 at 10:07
  • By the way. Your Java code can't be correct as you do not return a boolean value in the methods. I tried to change it but when changing this I get other errors. – VanessaF Mar 14 '21 at 10:10
  • @VanessaF I have changed to void, sorry i can't help you out more. – Android Killer Mar 15 '21 at 02:41
  • Thanks for your comment. Unfortunately it still does not work – VanessaF Mar 17 '21 at 18:11
  • Here is the error message "Class 'Anonymous class derived from RequestListener' must either be declared abstract or implement abstract method 'onLoadFailed(GlideException, Object, Target, boolean)' in 'RequestListener'" --> It is the same as before. And when I implement those methods I get another error message. So my question is does this code really work in your application WITHOUT any other glide specific command (I somehow question this) – VanessaF Mar 17 '21 at 18:14
-1

Here is the method mentioned above:

Glide.with(this)
  .load(url)
  .into(new SimpleTarget<>() {
    @Override
    public void onResourceReady(Drawable resource, Transition<GifDrawable> transition) {
      // Set the resource wherever you need to use it.
    binding.toolbar.setBackground(resource);
    }
  });
Sponge Bob
  • 821
  • 7
  • 3
  • Thanks SpongeBob for your answer. When using your suggested code I get 2 error messages. 1: "Class 'Anonymous class derived from SimpleTarget' must either be declared abstract or implement abstract method 'onResourceReady(R, Transition super R>)' in 'Target'", 2: "Method does not override method from its superclass" – VanessaF Mar 06 '21 at 09:05
  • Thanks SpongeBob. Any further comments to my last comments (or my update)? I'd highly appreciate every further comment from you – VanessaF Mar 07 '21 at 08:41