我想使用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'"
我想使用此依赖项,因为这是最新版本。
任何人都可以在这方面帮助我。提前谢谢。
答案 0 :(得分:15)
Bitmap chefBitmap = Glide.with(MyActivity.this)
.asBitmap()
.load(chef_image)
.submit()
.get();
答案 1 :(得分:7)
根据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();
它正在工作,我正在从侦听器获取位图。
答案 2 :(得分: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;
}
});
答案 3 :(得分:0)
您应该添加
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'
另外,在manifest.xml中授予权限
答案 4 :(得分:0)
在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);