在安卓中使用Glide从图像获取位图



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

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

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

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

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

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

根据最新版本的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();

它正在工作,我正在从侦听器那里获得位图。

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

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;
}
});

你应该添加你的

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'

此外,请在清单中授予权限.xml

在你的build.gradle中试试这个;

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

并像下面这样加载您的位图;

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

最新更新