有没有办法将图像作为位图加载到 Glide
Is there a way to load image as bitmap to Glide
我正在寻找一种使用位图作为 Glide 输入的方法。我什至不确定它是否可能。这是为了调整大小。 Glide 有很好的带缩放的图像增强。问题是我已经将位图资源加载到内存中。我能找到的唯一解决方案是将图像存储到临时文件并将它们重新加载回 Glide 作为 inputStream/file.. 有更好的方法来实现吗?
请在回答之前..我不是在谈论 Glide 的输出...asBitmap().get()
我知道that.I需要输入方面的帮助。
这是我的解决方案:
Bitmap bitmapNew=null;
try {
//
ContextWrapper cw = new ContextWrapper(ctx);
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File file=new File(directory,"temp.jpg");
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
//
bitmapNew = Glide
.with(ctx)
.load(file)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into( mActualWidth, mActualHeight - heightText)
.get();
file.delete();
} catch (Exception e) {
Logcat.e( "File not found: " + e.getMessage());
}
我想避免将图像写入内部并加载它们again.That 是我问是否有办法将输入作为位图的原因
谢谢
一个非常奇怪的案例,但让我们尝试解决它。我正在使用旧的且不酷 Picasso
,但总有一天我会试一试 Glide。
以下是一些可以帮助您的链接:
实际上是一种残酷但我认为有效的解决方法:
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);
我不确定这是否对你有帮助,但我希望它能让你离解决问题更近一步。
这是另一种解决方案,return 将位图设置到 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);
}
});
接受的答案适用于以前的版本,但在 Glide 的新版本中使用:
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(android.R.drawable.waiting);
requestOptions.error(R.drawable.waiting);
Glide.with(getActivity()).apply(requestOptions).load(imageUrl).into(imageView);
对于版本 4,您必须在 load()
之前调用 asBitmap()
GlideApp.with(itemView.getContext())
.asBitmap()
.load(data.getImageUrl())
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {}
});
}
物有所值,根据以上帖子,我的做法是:
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri imageUri = Uri.withAppendedPath(sArtworkUri, String.valueOf(album_id));
然后在适配器中:
// loading album cover using Glide library
Glide.with(mContext)
.asBitmap()
.load(imageUri)
.into(holder.thumbnail);
此解决方案适用于 Glide V4。
你可以这样得到位图:
Bitmap bitmap = Glide
.with(context)
.asBitmap()
.load(uri_File_String_Or_ResourceId)
.submit()
.get();
注意:这会阻塞当前线程加载图片。
根据 Glide
的最新版本,变化不大。现在我们需要使用 submit()
将图像作为位图加载,如果你不这样做 class submit()
那么监听器将不会被调用。
这是我今天使用的工作示例。
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();
它正在运行,我正在从侦听器获取位图。
在 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)
请为此使用实现:
implementation 'com.github.bumptech.glide:glide:4.9.0'
Glide.with(this)
.asBitmap()
.load("http://url")
.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) {
}
});
Glide 的大多数 API 和方法现已弃用。
以下适用于 Glide 4.9 和高达 Android 10.
对于图像 URI
Bitmap bitmap = Glide
.with(context)
.asBitmap()
.load(image_uri_or_drawable_resource_or_file_path)
.submit()
.get();
在build.gradle
中使用Glide如下
implementation 'com.github.bumptech.glide:glide:4.9.0'
这在最新版本的 Glide 中对我有用:
Glide.with(this)
.load(bitmap)
.dontTransform()
.into(imageView);
更新答案 2021 年 8 月
Glide.with(context)
.asBitmap()
.load(uri)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
useIt(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
onResourceReady : 资源加载完成时调用的方法。
resource
参数为加载的资源
onLoadCleared :在取消加载并释放其资源时调用的强制性生命周期回调。在重绘容器(通常是视图)或更改其可见性之前,您必须确保不再使用 onResourceReady 中收到的任何当前 Drawable。
placeholder
参数是可选显示的可绘制占位符,或者为空。
2021 年:
val bitmap=Glide.with(this).asBitmap().load(imageUri).submit().get()
我正在寻找一种使用位图作为 Glide 输入的方法。我什至不确定它是否可能。这是为了调整大小。 Glide 有很好的带缩放的图像增强。问题是我已经将位图资源加载到内存中。我能找到的唯一解决方案是将图像存储到临时文件并将它们重新加载回 Glide 作为 inputStream/file.. 有更好的方法来实现吗?
请在回答之前..我不是在谈论 Glide 的输出...asBitmap().get()
我知道that.I需要输入方面的帮助。
这是我的解决方案:
Bitmap bitmapNew=null;
try {
//
ContextWrapper cw = new ContextWrapper(ctx);
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File file=new File(directory,"temp.jpg");
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
//
bitmapNew = Glide
.with(ctx)
.load(file)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into( mActualWidth, mActualHeight - heightText)
.get();
file.delete();
} catch (Exception e) {
Logcat.e( "File not found: " + e.getMessage());
}
我想避免将图像写入内部并加载它们again.That 是我问是否有办法将输入作为位图的原因
谢谢
一个非常奇怪的案例,但让我们尝试解决它。我正在使用旧的且不酷 Picasso
,但总有一天我会试一试 Glide。
以下是一些可以帮助您的链接:
实际上是一种残酷但我认为有效的解决方法:
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);
我不确定这是否对你有帮助,但我希望它能让你离解决问题更近一步。
这是另一种解决方案,return 将位图设置到 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);
}
});
接受的答案适用于以前的版本,但在 Glide 的新版本中使用:
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(android.R.drawable.waiting);
requestOptions.error(R.drawable.waiting);
Glide.with(getActivity()).apply(requestOptions).load(imageUrl).into(imageView);
对于版本 4,您必须在 load()
asBitmap()
GlideApp.with(itemView.getContext())
.asBitmap()
.load(data.getImageUrl())
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {}
});
}
物有所值,根据以上帖子,我的做法是:
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri imageUri = Uri.withAppendedPath(sArtworkUri, String.valueOf(album_id));
然后在适配器中:
// loading album cover using Glide library
Glide.with(mContext)
.asBitmap()
.load(imageUri)
.into(holder.thumbnail);
此解决方案适用于 Glide V4。 你可以这样得到位图:
Bitmap bitmap = Glide
.with(context)
.asBitmap()
.load(uri_File_String_Or_ResourceId)
.submit()
.get();
注意:这会阻塞当前线程加载图片。
根据 Glide
的最新版本,变化不大。现在我们需要使用 submit()
将图像作为位图加载,如果你不这样做 class submit()
那么监听器将不会被调用。
这是我今天使用的工作示例。
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();
它正在运行,我正在从侦听器获取位图。
在 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)
请为此使用实现:
implementation 'com.github.bumptech.glide:glide:4.9.0'
Glide.with(this)
.asBitmap()
.load("http://url")
.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) {
}
});
Glide 的大多数 API 和方法现已弃用。 以下适用于 Glide 4.9 和高达 Android 10.
对于图像 URI
Bitmap bitmap = Glide
.with(context)
.asBitmap()
.load(image_uri_or_drawable_resource_or_file_path)
.submit()
.get();
在build.gradle
中使用Glide如下implementation 'com.github.bumptech.glide:glide:4.9.0'
这在最新版本的 Glide 中对我有用:
Glide.with(this)
.load(bitmap)
.dontTransform()
.into(imageView);
更新答案 2021 年 8 月
Glide.with(context)
.asBitmap()
.load(uri)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
useIt(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
onResourceReady : 资源加载完成时调用的方法。
resource
参数为加载的资源
onLoadCleared :在取消加载并释放其资源时调用的强制性生命周期回调。在重绘容器(通常是视图)或更改其可见性之前,您必须确保不再使用 onResourceReady 中收到的任何当前 Drawable。
placeholder
参数是可选显示的可绘制占位符,或者为空。
2021 年:
val bitmap=Glide.with(this).asBitmap().load(imageUri).submit().get()