【问题标题】:Is there a way to load image as bitmap to Glide有没有办法将图像作为位图加载到 Glide
【发布时间】:2017-07-05 19:28:11
【问题描述】:

我正在寻找一种使用位图作为 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());
        }

我想避免将图像写入内部并再次加载它们。这就是我问是否有办法将输入作为位图的原因

谢谢

【问题讨论】:

  • 你想用这个达到什么目的?
  • 我写的有问题......缩放..我没有找到比 Glide 更好的方法来缩放我的图像
  • 但究竟是什么,你的用例是什么
  • 我没有 -1,不,不清楚,因为您可以根据您想要实现的目标以多种方式缩放和播放图像
  • 我尝试了几十种缩放方法..最好的是 Glide。

标签: android image-resizing android-glide image-scaling


【解决方案1】:

一个非常奇怪的案例,但让我们尝试解决它。我正在使用旧的而不是酷的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 从 url 等加载图像吗?
  • “高效”:UI 线程上的 I/O?压缩只是为了解压?改为使用 POC 将其挤过管道。即使只是在创建图像时直接在后台调用转换也会更好。
  • @YuriiTsap 我正在从内存中视图的位图截屏。这些视图不仅仅是图像..
  • @YuriiTsap 我有理由回答你的问题。我使用 XML RPC odoo api,它只返回 base64 json 字符串格式的图像。
【解决方案2】:

这是另一种解决方案,它会返回一个位图以设置到您的 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);
                }
            });

【讨论】:

    【解决方案3】:

    接受的答案适用于以前的版本,但在 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);
    

    Courtesy

    【讨论】:

    • 不要对单个请求使用 setDefaultRequestOptions。仅当您想对特定 Activity 或 Fragment 中的所有请求应用相同的选项时,它才有用。要将选项应用于特定请求,请使用 .apply(RequestOptions):bumptech.github.io/glide/doc/options.html#requestoptions
    【解决方案4】:

    对于版本 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) {}
                });
            }
    

    更多信息:http://bumptech.github.io/glide/doc/targets.html

    【讨论】:

    • 哦 Glide,为什么要更改它并导致人们搜索错误数小时:/
    • SimpleTarget 在最新版本中已弃用
    • 我会接受这个答案,因为它是正在进行的版本的最新代码.. 谢谢@teffi
    • 请将此修复视为基本修复:stackoverflow.com/a/42278907/2267723,这是它的最新编码
    • SimpleTarget 已弃用。应替换为CustomTarget,如图here
    【解决方案5】:

    对于什么是值得的,基于上面的帖子,我的方法:

         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);
    

    【讨论】:

      【解决方案6】:

      此解决方案适用于 Glide V4。 你可以像这样得到位图:

      Bitmap bitmap = Glide
          .with(context)
          .asBitmap()
          .load(uri_File_String_Or_ResourceId)
          .submit()
          .get();
      

      注意:这会阻塞当前线程加载图片。

      【讨论】:

      • 它不起作用....有没有其他解决方案可以将位图作为 byte[] 的输出获取
      • 2021 年完美运行。
      • 正是我想要的。 95% 的其他答案总是用于加载到 Imageview。
      【解决方案7】:

      根据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();
      

      它正在工作,我正在从侦听器获取位图。

      【讨论】:

        【解决方案8】:

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

        【讨论】:

          【解决方案9】:

          请使用实现:

          实现'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) { 
           } 
          });
          

          【讨论】:

            【解决方案10】:

            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'
            

            【讨论】:

            • 此方法返回错误以将其运行到后台线程中。
            【解决方案11】:

            这在最新版本的 Glide 中对我有用:

            Glide.with(this)
                    .load(bitmap)
                    .dontTransform()
                    .into(imageView);
            

            【讨论】:

              【解决方案12】:

              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 参数是可绘制的占位符,可选择显示,或者为空。

              【讨论】:

                【解决方案13】:

                2021 年:

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

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-04-14
                  • 2010-11-11
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2020-05-29
                  • 2015-10-30
                  相关资源
                  最近更新 更多