简体   繁体   中英

Glide does not display picture in a Fragment in Android

I just started to use Glide for loading images into my Android App. I use it within a Fragment and I would like to set the Background of a toolbar to a certain figure from an URL. Unfortunately glide does not show the picture but just a white background in the toolbar. The toolbar itself is not the problem as I can change its background with a color and this works. Also displaying a saved image from the App folder (I stored it in the drawable folder) works.

Here is my code which is executed inside the onCreateView method of a fragment:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentGenericBinding.inflate(inflater, container, false);




        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.toolbarGenericMenu.setBackground(new BitmapDrawable(getResources(), bmap));

        //This code works at creates a green background
        //ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(getContext(), R.color.colorGreen));
        //binding.toolbarGenericDrinkMenu.setBackground(colorDrawable);


       //This code also works and display an image from R.drawable on the toolbar
       // binding.toolbarGenericMenu.setBackgroundResource(R.drawable.test_foto);




        return binding.getRoot();
    }

Any idea why loading the image with Glide does not work? I'd appreciate every comment.

You can try this if you are using androidx.appcompat.widget.Toolbar in layout in Kotlin

val toolbar = findViewById<Toolbar>(R.id.mToolbar)

        Glide.with(this)
                .asBitmap()
                .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(object : CustomTarget<Bitmap>() {
                    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                        toolbar.background = BitmapDrawable(resources, resource)
                    }
                    override fun onLoadCleared(placeholder: Drawable?) {}
                })

Java

Toolbar toolbar = findViewById(R.id.mToolbar);
        Glide.with(this)
                .asBitmap()
                .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(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        toolbar.setBackground(new BitmapDrawable(getResources(), resource));

                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM