当前位置: 首页 > 知识库问答 >
问题:

如何使用glide将图像下载到位图中?

益稳
2023-03-14

使用Glide将URL下载到ImageView非常容易:

Glide
   .with(context)
   .load(getIntent().getData())
   .placeholder(R.drawable.ic_loading)
   .centerCrop()
   .into(imageView);

我想知道我是否也可以下载到位图中?我想下载到原始位图中,然后我可以使用其他工具进行操作。我已经浏览了代码,但不知道如何操作。

共有3个答案

单于经纬
2023-03-14

覆盖目标类或BitmapImageViewTarget之类的实现,并覆盖setResource方法来捕获位图可能是一种方法。。。

这未经测试。:-)

    Glide.with(context)
         .load("http://goo.gl/h8qOq7")
         .asBitmap()
         .into(new BitmapImageViewTarget(imageView) {
                     @Override
                     protected void setResource(Bitmap resource) {
                         // Do bitmap magic here
                         super.setResource(resource);
                     }
         });
阎宾实
2023-03-14
匿名用户

我对Glide不够熟悉,但看起来如果你知道目标大小,你可以使用这样的东西:

Bitmap theBitmap = Glide.
        with(this).
        load("http://....").
        asBitmap().
        into(100, 100). // Width and height
        get();

看起来你可以通过-1,-1,得到一个全尺寸的图像(纯粹基于测试,看不到文档)。

注:into(int,int)返回一个未来目标

class SomeActivity extends Activity {

    private Bitmap theBitmap = null;
        
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // onCreate stuff ...
        final ImageView image = (ImageView) findViewById(R.id.imageView);

        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                Looper.prepare();
                try {
                    theBitmap = Glide.
                        with(SomeActivity.this).
                        load("https://www.google.es/images/srpr/logo11w.png").
                        asBitmap().
                        into(-1,-1).
                        get();
                 } catch (final ExecutionException e) {
                     Log.e(TAG, e.getMessage());
                 } catch (final InterruptedException e) {
                     Log.e(TAG, e.getMessage());
                 }
                 return null;
            }
            @Override
            protected void onPostExecute(Void dummy) {
                if (null != theBitmap) {
                    // The full bitmap should be available here
                    image.setImageBitmap(theBitmap);
                    Log.d(TAG, "Image loaded");
                };
            }
        }.execute();
    }
}

根据Monkeyless在下面评论中的建议(这似乎也是官方的方式),您可以使用一个简单的获取,可以选择与覆盖(int,int)结合使用,以大大简化代码。但是,在这种情况下,必须提供准确的尺寸(不接受低于1的尺寸):

Glide
    .with(getApplicationContext())
    .load("https://www.google.es/images/srpr/logo11w.png")
    .asBitmap()
    .into(new SimpleTarget<Bitmap>(100,100) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
            image.setImageBitmap(resource); // Possibly runOnUiThread()
        }
    });

正如@hennry所建议的,如果您需要相同的图像,请使用new SimpleTarget

更新

 bitmap = Glide.with(c).asBitmap().load( "url").submit().get();

姜博
2023-03-14

确保您使用的是最新版本

实现“com.github.bumptech.glide:glide:4.10.0”

科特林:

Glide.with(this)
        .asBitmap()
        .load(imagePath)
        .into(object : CustomTarget<Bitmap>(){
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                imageView.setImageBitmap(resource)
            }
            override fun onLoadCleared(placeholder: Drawable?) {
                // this is called when imageView is cleared on lifecycle call or for
                // some other reason.
                // if you are referencing the bitmap somewhere else too other than this imageView
                // clear it here as you can no longer have the bitmap
            }
        })

位图大小:

如果要使用图像的原始大小,请使用上述默认构造函数,否则可以传递所需的位图大小

进入(对象:CustomTarget

Java:

Glide.with(this)
        .asBitmap()
        .load(path)
        .into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                imageView.setImageBitmap(resource);
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {
            }
        });

旧答案:

使用编译com.github.bumptech.glide:glide:4.8.0及其以下版本

Glide.with(this)
        .asBitmap()
        .load(path)
        .into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                imageView.setImageBitmap(resource);
            }
        });

对于编译'com.github.bumptech.glide: glide: 3.7.0'及以下

Glide.with(this)
        .load(path)
        .asBitmap()
        .into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                imageView.setImageBitmap(resource);
            }
        });

现在您可能会看到一个警告SimpleTarget已弃用

原因:

反对SimpleTarget的主要目的是警告您它以何种方式诱使您破坏Glide的API契约。具体来说,一旦SimpleTarget被清除,它不会迫使您停止使用任何已加载的资源,这可能会导致崩溃和图形损坏。

只要您确保在ImageView被清除后不使用位图,SimpleTarget仍然可以使用。

 类似资料:
  • 问题内容: 如何使用Glide库将位图加载到ImageView中?我想使用文本创建自定义图像,然后使用Glide将其加载到imageview中。 这是我用文本创建自定义位图的方法 But when i try to load this bitmap using glide i’m getting error 问题答案: @rookiedev是正确的, Glide中没有load(Bitmap)它,这

  • 如何使用Glide库将位图加载到ImageView中?我想用文本创建一个自定义图像,并使用Glide将其加载到imageview中。 这是我用文本创建自定义位图的方法 但是当我尝试使用glide加载这个位图时,我得到了一个错误

  • 我使用滑翔库来显示网格视图中的图像,但是在我的图像视图中显示了注释。 E/Glide:class com。邦普泰克。滑行负载发动机GlideException:无法加载资源 我的代码在使用位图时工作正常。这是我的密码:

  • 我正在使用Glide库在imageview中加载图像,并使用以下代码。 灰色占位符在图像未加载之前可见,但在图像在imageview中加载后,占位符仍会出现,并在该图像后显示一些空白。 我如何解决这个问题。如果你有任何想法,请帮助我。

  • 我正在尝试加载带有数据绑定的图像。但我一直没有忘记。我的问题在哪里?下面是我的代码和布局结构。 MyItemViewModel。kt 布局xml

  • 我知道如何使用滑翔将图像插入到常规ImageView中 (