有没有一种方法可以将图像作为位图加载到Glide中?

46

我正在寻找一种将位图用作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());
        }
我希望避免将图像写入内部并再次加载它们。这就是我询问是否有方法可以将输入作为位图的原因。
谢谢。

1
你想通过这个实现什么目标? - Drilon Blakqori
我在问题中写道...缩放...我没有找到比Glide更好的图像缩放方式。 - Maher Abuthraa
但是具体来说,你的使用情况是什么? - Drilon Blakqori
我没有减1,而且不是很清楚,因为您可以根据想要实现的目标以许多方式缩放和处理图像。 - Drilon Blakqori
我尝试了数十种缩放方法.. 最佳的是 Glide。 - Maher Abuthraa
13个回答

2

2022年8月更新的答案

Glide.with(context)
      .asBitmap()
      .load(uri) // Uri, String, File...
      .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 : 一个必须的生命周期回调,在取消加载并释放其资源时调用。您必须确保在重新绘制容器(通常是View)或更改其可见性之前,不再使用onResourceReady中接收到的任何当前Drawable。
参数placeholder是要显示的占位符drawable,或为null。


1

对于2021年:

  val bitmap=Glide.with(this).asBitmap().load(imageUri).submit().get()

1
在 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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接