简体   繁体   English

在Android中使用Glide从Image获取Bitmap

[英]Getting Bitmap from Image using Glide in android

I want to get the bitmap from an image using Glide. 我想使用Glide从图像中获取位图。 I am doing the following - 我正在做以下事情 -

Bitmap chefBitmap = Glide.with(MyActivity.this)
.load(chef_image)
.asBitmap()
.into(100, 100)
.get();

It used to work with the previous Glide version. 它曾用于以前的Glide版本。 But it does not work with this in gradle - "compile 'com.github.bumptech.glide:glide:4.0.0'" 但它在gradle中无效 - "compile 'com.github.bumptech.glide:glide:4.0.0'"

I want to use this dependency because this is the latest version. 我想使用此依赖项,因为这是最新版本。

Can anyone help me in this regard. 谁能在这方面帮助我。 Thanks in advance. 提前致谢。

Bitmap chefBitmap = Glide.with(MyActivity.this)
.asBitmap()
.load(chef_image)
.submit()
.get();

There is little changes according to latest version of Glide . 根据Glide最新版本,几乎没有变化。 Now we need to use submit() to load image as bitmap, if you do not call submit() than listener won't be called. 现在我们需要使用submit()将图像作为位图加载,如果不调用submit() ,则不会调用侦听器。 In 4.0 Version, submit() added and in order to invoke listener. 在4.0版本中,添加了submit()并调用了侦听器。 One of user commented code is working with GlideApp . 用户注释的代码之一正在与GlideApp You can use below code to run with GlideApp if you are using. 如果您使用,可以使用以下代码与GlideApp一起运行。

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 im getting bitmap from listener. 它正在工作,我从侦听器获取位图。

You need to set the size with RequestOptions in apply() and use a RequestListener to retrieve the bitmap. 您需要在apply()中使用RequestOptions设置大小,并使用RequestListener来检索位图。 The mthod asBitmap() needs to be called before load(). 需要在load()之前调用mthod asBitmap()。 So it will look like this: 所以它看起来像这样:

Glide.with(getContext().getApplicationContext())
    .asBitmap()
    .load(chef_image)
    .apply(new RequestOptions().override(100, 100))
    .listener(new RequestListener<Bitmap>() {
        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
            // resource is your loaded Bitmap
            return true;
        }
    });

You should add in your 你应该加入你的

dependencies{
  compile 'com.github.bumptech.glide:glide:4.0.0'
  compile 'com.android.support:support-v4:25.3.1'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0'

Also, give permission in your manifest.xml 另外,在manifest.xml中授予权限

Try this in your build.gradle; 在build.gradle中尝试这个;

 compile 'com.github.bumptech.glide:glide:3.7.0'

and load your bitmap like below; 并加载你的位图,如下所示;

  Glide.with(activity).load(m.getThumbnailUrl())
            .thumbnail(0.5f)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(imageview);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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