我正在寻找一种方法来使用位图作为Glide的输入。我甚至不确定它是否可能。这是为了调整大小的目的。 Glide具有良好的图像增强功能。问题是我有资源作为位图已经加载到内存。我能找到的唯一解决方案是将图像存储到临时文件中,并将它们作为inputStream / file重新加载回Glide ..有没有更好的方法来实现它?
请在回答之前..我不是在谈论Glide的输出.. .asBitmap().get()
我知道。我需要帮助输入。
这是我的解决方案:
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());
}
我想避免将图像写入内部并再次加载它们。这就是为什么我要问是否有办法将输入作为位图
谢谢
对于版本4,你必须在asBitmap()
之前调用load()
GlideApp.with(itemView.getContext())
.asBitmap()
.load(data.getImageUrl())
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {}
});
}
一个非常奇怪的案例,但我们试着解决它。我正在使用旧的而不是很酷的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);
我不确定这是否会对您有所帮助,但我希望它能让您更接近解决方案。
此解决方案适用于Glide V4。您可以像这样获取位图:
Bitmap bitmap = Glide
.with(context)
.asBitmap()
.load(uri_File_String_Or_ResourceId)
.submit()
.get();
注意:这将阻止当前线程加载图像。
根据Glide
的最新版本,几乎没有变化。现在我们需要使用submit()
将图像加载为位图,如果你没有调用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();
它正在工作,我从侦听器获取位图。
接受的答案适用于以前的版本,但在新版本的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);
这是另一个解决方案,它返回一个位图以设置到您的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);
}
});
在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)
根据上面的帖子,我的方法是值得的:
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);
请使用实施,即:
实现'com.github.bumptech.glide:glide:4.9.0'
Glide.with(this)
.asBitmap()
.load(item.getMarkertOverlayImage())
.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) {
}
});