在Android中使用Glide从Image获取Bitmap

Zac*_*ary 9 android bitmap imageview android-glide

我想使用Glide从图像中获取位图.我正在做以下事情 -

Bitmap chefBitmap = Glide.with(MyActivity.this)
.load(chef_image)
.asBitmap()
.into(100, 100)
.get();
Run Code Online (Sandbox Code Playgroud)

它曾用于以前的Glide版本.但它在gradle中不起作用 - "compile 'com.github.bumptech.glide:glide:4.0.0'"

我想使用此依赖项,因为这是最新版本.

谁能在这方面帮助我.提前致谢.

Ana*_*ara 19

Bitmap chefBitmap = Glide.with(MyActivity.this)
.asBitmap()
.load(chef_image)
.submit()
.get();
Run Code Online (Sandbox Code Playgroud)


Ash*_*mar 11

根据最新版本,几乎没有变化Glide.现在我们需要使用submit()将图像加载为位图,如果不调用则不会调用submit()侦听器.在4.0版本中,submit()添加并调用侦听器.用户注释的代码之一正在使用GlideApp.如果您使用,可以使用以下代码与GlideApp一起运行.

这是我今天使用的工作示例.

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();
Run Code Online (Sandbox Code Playgroud)

它正在工作,我从侦听器获取位图.


Bjö*_*hel 6

您需要在apply()中使用RequestOptions设置大小,并使用RequestListener来检索位图.需要在load()之前调用mthod asBitmap().所以它看起来像这样:

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;
        }
    });
Run Code Online (Sandbox Code Playgroud)


Zaf*_*nov 1

你应该添加你的

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'
Run Code Online (Sandbox Code Playgroud)

另外,在你的manifest.xml中授予权限