Is there a way to load image as bitmap to Glide

孤街浪徒 提交于 2019-11-27 19:23:15

For version 4 you have to call asBitmap() before load()

GlideApp.with(itemView.getContext())
        .asBitmap()
        .load(data.getImageUrl())
        .into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {}
            });
        }

More info: http://bumptech.github.io/glide/doc/targets.html

Yurii Tsap

A really strange case, but lets try to solve it. I'm using the old and not cool Picasso, but one day I'll give Glide a try. Here are some links that could help you :

And actually a cruel but I think efficient way to solve this :

ByteArrayOutputStream stream = new ByteArrayOutputStream();
  yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
  Glide.with(this)
      .load(stream.toByteArray())
      .asBitmap()
      .error(R.drawable.ic_thumb_placeholder)
      .transform(new CircleTransform(this))
      .into(imageview);

I'm not sure if this will help you, but I hope it can make you a step closer to the solution.

This solution is working with Glide V4. You can get the bitmap like this:

Bitmap bitmap = Glide
    .with(context)
    .asBitmap()
    .load(uri_File_String_Or_ResourceId)
    .submit()
    .get();

Note: this will block the current thread to load the image.

Ashu Kumar

There is little changes according to latest version of Glide. Now we need to use submit() to load image as bitmap, if you do not class submit() than listener won't be called.

here is working example i used today.

Glide.with(cxt)
  .asBitmap().load(imageUrl)
  .listener(new RequestListener<Bitmap>() {
      @Override
      public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Bitmap> target, boolean b) {
          Toast.makeText(cxt,getResources().getString(R.string.unexpected_error_occurred_try_again),Toast.LENGTH_SHORT).show();
          return false;
      }

      @Override
      public boolean onResourceReady(Bitmap bitmap, Object o, Target<Bitmap> target, DataSource dataSource, boolean b) {
          zoomImage.setImage(ImageSource.bitmap(bitmap));
          return false;
      }
  }
).submit();

It is working and I'm getting bitmap from listener.

Safeer

The accepted answer works for previous versions, but in new versions of Glide use:

RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(android.R.drawable.waiting);
requestOptions.error(R.drawable.waiting);
Glide.with(getActivity()).apply(requestOptions).load(imageUrl).into(imageView);

Courtesy

here's another solution which return you a bitmap to set into your ImageView

Glide.with(this)
            .load(R.drawable.card_front)    // you can pass url too
            .asBitmap()
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    // you can do something with loaded bitmap here

                    imgView.setImageBitmap(resource);
                }
            });

In Kotlin,

Glide.with(this)
            .asBitmap()
            .load("https://...")
            .addListener(object : RequestListener<Bitmap> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Bitmap>?,
                    isFirstResource: Boolean
                ): Boolean {
                    Toast.makeText(this@MainActivity, "failed: " + e?.printStackTrace(), Toast.LENGTH_SHORT).show()
                    return false
                }

                override fun onResourceReady(
                    resource: Bitmap?,
                    model: Any?,
                    target: Target<Bitmap>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    //image is ready, you can get bitmap here
                    return false
                }

            })
            .into(imageView)

For what is is worth, based upon the posts above, my approach:

     Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri imageUri = Uri.withAppendedPath(sArtworkUri, String.valueOf(album_id));

then in the adapter:

        //  loading album cover using Glide library

    Glide.with(mContext)
            .asBitmap()
            .load(imageUri)
            .into(holder.thumbnail);

Please use Implementation for that is:

implementation 'com.github.bumptech.glide:glide:4.9.0'

     Glide.with(this)
     .asBitmap()
      .load(item.getMarkertOverlayImage())
    .into(new CustomTarget <Bitmap>() {   
@Override  
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition <? super Bitmap> transition) { 
                // you can do something with loaded bitmap here

 }
@Override 
public void onLoadCleared(@Nullable Drawable placeholder) { 
 } 
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!